fimc-is-sensor.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Samsung EXYNOS4x12 FIMC-IS (Imaging Subsystem) driver
  3. *
  4. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  5. *
  6. * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/errno.h>
  15. #include <linux/gpio.h>
  16. #include <linux/i2c.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/slab.h>
  23. #include <media/v4l2-subdev.h>
  24. #include "fimc-is.h"
  25. #include "fimc-is-sensor.h"
  26. #define DRIVER_NAME "FIMC-IS-SENSOR"
  27. static const char * const sensor_supply_names[] = {
  28. "svdda",
  29. "svddio",
  30. };
  31. static const struct v4l2_mbus_framefmt fimc_is_sensor_formats[] = {
  32. {
  33. .code = V4L2_MBUS_FMT_SGRBG10_1X10,
  34. .colorspace = V4L2_COLORSPACE_SRGB,
  35. .field = V4L2_FIELD_NONE,
  36. }
  37. };
  38. static struct fimc_is_sensor *sd_to_fimc_is_sensor(struct v4l2_subdev *sd)
  39. {
  40. return container_of(sd, struct fimc_is_sensor, subdev);
  41. }
  42. static const struct v4l2_mbus_framefmt *find_sensor_format(
  43. struct v4l2_mbus_framefmt *mf)
  44. {
  45. int i;
  46. for (i = 0; i < ARRAY_SIZE(fimc_is_sensor_formats); i++)
  47. if (mf->code == fimc_is_sensor_formats[i].code)
  48. return &fimc_is_sensor_formats[i];
  49. return &fimc_is_sensor_formats[0];
  50. }
  51. static int fimc_is_sensor_enum_mbus_code(struct v4l2_subdev *sd,
  52. struct v4l2_subdev_fh *fh,
  53. struct v4l2_subdev_mbus_code_enum *code)
  54. {
  55. if (code->index >= ARRAY_SIZE(fimc_is_sensor_formats))
  56. return -EINVAL;
  57. code->code = fimc_is_sensor_formats[code->index].code;
  58. return 0;
  59. }
  60. static void fimc_is_sensor_try_format(struct fimc_is_sensor *sensor,
  61. struct v4l2_mbus_framefmt *mf)
  62. {
  63. const struct sensor_drv_data *dd = sensor->drvdata;
  64. const struct v4l2_mbus_framefmt *fmt;
  65. fmt = find_sensor_format(mf);
  66. mf->code = fmt->code;
  67. v4l_bound_align_image(&mf->width, 16 + 8, dd->width, 0,
  68. &mf->height, 12 + 8, dd->height, 0, 0);
  69. }
  70. static struct v4l2_mbus_framefmt *__fimc_is_sensor_get_format(
  71. struct fimc_is_sensor *sensor, struct v4l2_subdev_fh *fh,
  72. u32 pad, enum v4l2_subdev_format_whence which)
  73. {
  74. if (which == V4L2_SUBDEV_FORMAT_TRY)
  75. return fh ? v4l2_subdev_get_try_format(fh, pad) : NULL;
  76. return &sensor->format;
  77. }
  78. static int fimc_is_sensor_set_fmt(struct v4l2_subdev *sd,
  79. struct v4l2_subdev_fh *fh,
  80. struct v4l2_subdev_format *fmt)
  81. {
  82. struct fimc_is_sensor *sensor = sd_to_fimc_is_sensor(sd);
  83. struct v4l2_mbus_framefmt *mf;
  84. fimc_is_sensor_try_format(sensor, &fmt->format);
  85. mf = __fimc_is_sensor_get_format(sensor, fh, fmt->pad, fmt->which);
  86. if (mf) {
  87. mutex_lock(&sensor->lock);
  88. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  89. *mf = fmt->format;
  90. mutex_unlock(&sensor->lock);
  91. }
  92. return 0;
  93. }
  94. static int fimc_is_sensor_get_fmt(struct v4l2_subdev *sd,
  95. struct v4l2_subdev_fh *fh,
  96. struct v4l2_subdev_format *fmt)
  97. {
  98. struct fimc_is_sensor *sensor = sd_to_fimc_is_sensor(sd);
  99. struct v4l2_mbus_framefmt *mf;
  100. mf = __fimc_is_sensor_get_format(sensor, fh, fmt->pad, fmt->which);
  101. mutex_lock(&sensor->lock);
  102. fmt->format = *mf;
  103. mutex_unlock(&sensor->lock);
  104. return 0;
  105. }
  106. static struct v4l2_subdev_pad_ops fimc_is_sensor_pad_ops = {
  107. .enum_mbus_code = fimc_is_sensor_enum_mbus_code,
  108. .get_fmt = fimc_is_sensor_get_fmt,
  109. .set_fmt = fimc_is_sensor_set_fmt,
  110. };
  111. static int fimc_is_sensor_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  112. {
  113. struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(fh, 0);
  114. *format = fimc_is_sensor_formats[0];
  115. format->width = FIMC_IS_SENSOR_DEF_PIX_WIDTH;
  116. format->height = FIMC_IS_SENSOR_DEF_PIX_HEIGHT;
  117. return 0;
  118. }
  119. static const struct v4l2_subdev_internal_ops fimc_is_sensor_sd_internal_ops = {
  120. .open = fimc_is_sensor_open,
  121. };
  122. static int fimc_is_sensor_s_power(struct v4l2_subdev *sd, int on)
  123. {
  124. struct fimc_is_sensor *sensor = v4l2_get_subdevdata(sd);
  125. int gpio = sensor->gpio_reset;
  126. int ret;
  127. if (on) {
  128. ret = pm_runtime_get(sensor->dev);
  129. if (ret < 0)
  130. return ret;
  131. ret = regulator_bulk_enable(SENSOR_NUM_SUPPLIES,
  132. sensor->supplies);
  133. if (ret < 0) {
  134. pm_runtime_put(sensor->dev);
  135. return ret;
  136. }
  137. if (gpio_is_valid(gpio)) {
  138. gpio_set_value(gpio, 1);
  139. usleep_range(600, 800);
  140. gpio_set_value(gpio, 0);
  141. usleep_range(10000, 11000);
  142. gpio_set_value(gpio, 1);
  143. }
  144. /* A delay needed for the sensor initialization. */
  145. msleep(20);
  146. } else {
  147. if (gpio_is_valid(gpio))
  148. gpio_set_value(gpio, 0);
  149. ret = regulator_bulk_disable(SENSOR_NUM_SUPPLIES,
  150. sensor->supplies);
  151. if (!ret)
  152. pm_runtime_put(sensor->dev);
  153. }
  154. pr_info("%s:%d: on: %d, ret: %d\n", __func__, __LINE__, on, ret);
  155. return ret;
  156. }
  157. static struct v4l2_subdev_core_ops fimc_is_sensor_core_ops = {
  158. .s_power = fimc_is_sensor_s_power,
  159. };
  160. static struct v4l2_subdev_ops fimc_is_sensor_subdev_ops = {
  161. .core = &fimc_is_sensor_core_ops,
  162. .pad = &fimc_is_sensor_pad_ops,
  163. };
  164. static const struct of_device_id fimc_is_sensor_of_match[];
  165. static int fimc_is_sensor_probe(struct i2c_client *client,
  166. const struct i2c_device_id *id)
  167. {
  168. struct device *dev = &client->dev;
  169. struct fimc_is_sensor *sensor;
  170. const struct of_device_id *of_id;
  171. struct v4l2_subdev *sd;
  172. int gpio, i, ret;
  173. sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
  174. if (!sensor)
  175. return -ENOMEM;
  176. mutex_init(&sensor->lock);
  177. sensor->gpio_reset = -EINVAL;
  178. gpio = of_get_gpio_flags(dev->of_node, 0, NULL);
  179. if (gpio_is_valid(gpio)) {
  180. ret = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_LOW,
  181. DRIVER_NAME);
  182. if (ret < 0)
  183. return ret;
  184. }
  185. sensor->gpio_reset = gpio;
  186. for (i = 0; i < SENSOR_NUM_SUPPLIES; i++)
  187. sensor->supplies[i].supply = sensor_supply_names[i];
  188. ret = devm_regulator_bulk_get(&client->dev, SENSOR_NUM_SUPPLIES,
  189. sensor->supplies);
  190. if (ret < 0)
  191. return ret;
  192. of_id = of_match_node(fimc_is_sensor_of_match, dev->of_node);
  193. if (!of_id)
  194. return -ENODEV;
  195. sensor->drvdata = of_id->data;
  196. sensor->dev = dev;
  197. sd = &sensor->subdev;
  198. v4l2_i2c_subdev_init(sd, client, &fimc_is_sensor_subdev_ops);
  199. snprintf(sd->name, sizeof(sd->name), sensor->drvdata->subdev_name);
  200. sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  201. sensor->format.code = fimc_is_sensor_formats[0].code;
  202. sensor->format.width = FIMC_IS_SENSOR_DEF_PIX_WIDTH;
  203. sensor->format.height = FIMC_IS_SENSOR_DEF_PIX_HEIGHT;
  204. sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
  205. ret = media_entity_init(&sd->entity, 1, &sensor->pad, 0);
  206. if (ret < 0)
  207. return ret;
  208. v4l2_set_subdevdata(sd, sensor);
  209. pm_runtime_no_callbacks(dev);
  210. pm_runtime_enable(dev);
  211. return ret;
  212. }
  213. static int fimc_is_sensor_remove(struct i2c_client *client)
  214. {
  215. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  216. media_entity_cleanup(&sd->entity);
  217. return 0;
  218. }
  219. static const struct i2c_device_id fimc_is_sensor_ids[] = {
  220. { }
  221. };
  222. static const struct sensor_drv_data s5k6a3_drvdata = {
  223. .id = FIMC_IS_SENSOR_ID_S5K6A3,
  224. .subdev_name = "S5K6A3",
  225. .width = S5K6A3_SENSOR_WIDTH,
  226. .height = S5K6A3_SENSOR_HEIGHT,
  227. };
  228. static const struct of_device_id fimc_is_sensor_of_match[] = {
  229. {
  230. .compatible = "samsung,s5k6a3",
  231. .data = &s5k6a3_drvdata,
  232. },
  233. { }
  234. };
  235. static struct i2c_driver fimc_is_sensor_driver = {
  236. .driver = {
  237. .of_match_table = fimc_is_sensor_of_match,
  238. .name = DRIVER_NAME,
  239. .owner = THIS_MODULE,
  240. },
  241. .probe = fimc_is_sensor_probe,
  242. .remove = fimc_is_sensor_remove,
  243. .id_table = fimc_is_sensor_ids,
  244. };
  245. int fimc_is_register_sensor_driver(void)
  246. {
  247. return i2c_add_driver(&fimc_is_sensor_driver);
  248. }
  249. void fimc_is_unregister_sensor_driver(void)
  250. {
  251. i2c_del_driver(&fimc_is_sensor_driver);
  252. }
  253. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  254. MODULE_DESCRIPTION("Exynos4x12 FIMC-IS image sensor subdev driver");
  255. MODULE_LICENSE("GPL");