/drivers/media/video/msm/eeprom/msm_camera_eeprom.c

https://bitbucket.org/anders3408/kernel_oppo_find5 · C · 196 lines · 159 code · 24 blank · 13 comment · 29 complexity · 05c01a453c3b8cfa4f3486edd6630fe4 MD5 · raw file

  1. /* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include "msm_camera_eeprom.h"
  13. int32_t msm_camera_eeprom_read(struct msm_eeprom_ctrl_t *ectrl,
  14. uint32_t reg_addr, void *data, uint32_t num_byte,
  15. uint16_t convert_endian)
  16. {
  17. int rc = 0;
  18. if (ectrl->func_tbl.eeprom_set_dev_addr != NULL)
  19. ectrl->func_tbl.eeprom_set_dev_addr(ectrl, &reg_addr);
  20. if (!convert_endian) {
  21. rc = msm_camera_i2c_read_seq(
  22. &ectrl->i2c_client, reg_addr, data, num_byte);
  23. } else {
  24. unsigned char buf[num_byte];
  25. uint8_t *data_ptr = (uint8_t *) data;
  26. int i;
  27. rc = msm_camera_i2c_read_seq(
  28. &ectrl->i2c_client, reg_addr, buf, num_byte);
  29. for (i = 0; i < num_byte; i += 2) {
  30. data_ptr[i] = buf[i+1];
  31. data_ptr[i+1] = buf[i];
  32. }
  33. }
  34. return rc;
  35. }
  36. int32_t msm_camera_eeprom_read_tbl(struct msm_eeprom_ctrl_t *ectrl,
  37. struct msm_camera_eeprom_read_t *read_tbl, uint16_t tbl_size)
  38. {
  39. int i, rc = 0;
  40. CDBG("%s: open\n", __func__);
  41. if (read_tbl == NULL)
  42. return rc;
  43. for (i = 0; i < tbl_size; i++) {
  44. rc = msm_camera_eeprom_read
  45. (ectrl, read_tbl[i].reg_addr,
  46. read_tbl[i].dest_ptr, read_tbl[i].num_byte,
  47. read_tbl[i].convert_endian);
  48. if (rc < 0) {
  49. pr_err("%s: read failed\n", __func__);
  50. return rc;
  51. }
  52. }
  53. CDBG("%s: done\n", __func__);
  54. return rc;
  55. }
  56. int32_t msm_camera_eeprom_get_info(struct msm_eeprom_ctrl_t *ectrl,
  57. struct msm_camera_eeprom_info_t *einfo)
  58. {
  59. int rc = 0;
  60. CDBG("%s: open\n", __func__);
  61. memcpy(einfo, ectrl->info, ectrl->info_size);
  62. CDBG("%s: done =%d\n", __func__, rc);
  63. return rc;
  64. }
  65. int32_t msm_camera_eeprom_get_data(struct msm_eeprom_ctrl_t *ectrl,
  66. struct msm_eeprom_data_t *edata)
  67. {
  68. int rc = 0;
  69. if (edata->index >= ectrl->data_tbl_size)
  70. return -EFAULT;
  71. if (copy_to_user(edata->eeprom_data,
  72. ectrl->data_tbl[edata->index].data,
  73. ectrl->data_tbl[edata->index].size))
  74. rc = -EFAULT;
  75. return rc;
  76. }
  77. int32_t msm_eeprom_config(struct msm_eeprom_ctrl_t *e_ctrl,
  78. void __user *argp)
  79. {
  80. struct msm_eeprom_cfg_data cdata;
  81. int32_t rc = 0;
  82. if (copy_from_user(&cdata,
  83. (void *)argp,
  84. sizeof(struct msm_eeprom_cfg_data)))
  85. return -EFAULT;
  86. mutex_lock(e_ctrl->eeprom_mutex);
  87. switch (cdata.cfgtype) {
  88. case CFG_GET_EEPROM_INFO:
  89. if (e_ctrl->func_tbl.eeprom_get_info == NULL) {
  90. rc = -EFAULT;
  91. break;
  92. }
  93. rc = e_ctrl->func_tbl.eeprom_get_info(e_ctrl,
  94. &cdata.cfg.get_info);
  95. if (copy_to_user((void *)argp,
  96. &cdata,
  97. sizeof(struct msm_eeprom_cfg_data)))
  98. rc = -EFAULT;
  99. break;
  100. case CFG_GET_EEPROM_DATA:
  101. if (e_ctrl->func_tbl.eeprom_get_data == NULL) {
  102. rc = -EFAULT;
  103. break;
  104. }
  105. rc = e_ctrl->func_tbl.eeprom_get_data(e_ctrl,
  106. &cdata.cfg.get_data);
  107. if (copy_to_user((void *)argp,
  108. &cdata,
  109. sizeof(struct msm_eeprom_cfg_data)))
  110. rc = -EFAULT;
  111. break;
  112. default:
  113. break;
  114. }
  115. mutex_unlock(e_ctrl->eeprom_mutex);
  116. return rc;
  117. }
  118. struct msm_eeprom_ctrl_t *get_ectrl(struct v4l2_subdev *sd)
  119. {
  120. return container_of(sd, struct msm_eeprom_ctrl_t, sdev);
  121. }
  122. long msm_eeprom_subdev_ioctl(struct v4l2_subdev *sd,
  123. unsigned int cmd, void *arg)
  124. {
  125. struct msm_eeprom_ctrl_t *e_ctrl = get_ectrl(sd);
  126. void __user *argp = (void __user *)arg;
  127. switch (cmd) {
  128. case VIDIOC_MSM_EEPROM_CFG:
  129. return msm_eeprom_config(e_ctrl, argp);
  130. default:
  131. return -ENOIOCTLCMD;
  132. }
  133. }
  134. int32_t msm_eeprom_i2c_probe(struct i2c_client *client,
  135. const struct i2c_device_id *id)
  136. {
  137. int rc = 0;
  138. struct msm_eeprom_ctrl_t *e_ctrl_t = NULL;
  139. CDBG("%s called\n", __func__);
  140. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  141. pr_err("i2c_check_functionality failed\n");
  142. goto probe_failure;
  143. }
  144. e_ctrl_t = (struct msm_eeprom_ctrl_t *)(id->driver_data);
  145. e_ctrl_t->i2c_client.client = client;
  146. if (e_ctrl_t->i2c_addr != 0)
  147. e_ctrl_t->i2c_client.client->addr = e_ctrl_t->i2c_addr;
  148. CDBG("%s client = %x\n", __func__, (unsigned int) client);
  149. /* Assign name for sub device */
  150. snprintf(e_ctrl_t->sdev.name, sizeof(e_ctrl_t->sdev.name),
  151. "%s", e_ctrl_t->i2c_driver->driver.name);
  152. if (e_ctrl_t->func_tbl.eeprom_init != NULL) {
  153. rc = e_ctrl_t->func_tbl.eeprom_init(e_ctrl_t,
  154. e_ctrl_t->i2c_client.client->adapter);
  155. }
  156. msm_camera_eeprom_read_tbl(e_ctrl_t,
  157. e_ctrl_t->read_tbl,
  158. e_ctrl_t->read_tbl_size);
  159. if (e_ctrl_t->func_tbl.eeprom_format_data != NULL)
  160. e_ctrl_t->func_tbl.eeprom_format_data();
  161. if (e_ctrl_t->func_tbl.eeprom_release != NULL)
  162. rc = e_ctrl_t->func_tbl.eeprom_release(e_ctrl_t);
  163. /* Initialize sub device */
  164. v4l2_i2c_subdev_init(&e_ctrl_t->sdev,
  165. e_ctrl_t->i2c_client.client,
  166. e_ctrl_t->eeprom_v4l2_subdev_ops);
  167. CDBG("%s success resut=%d\n", __func__, rc);
  168. return rc;
  169. probe_failure:
  170. pr_err("%s failed! rc = %d\n", __func__, rc);
  171. return rc;
  172. }