fimc-is-sensor.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 const struct v4l2_mbus_framefmt *find_sensor_format(
  39. struct v4l2_mbus_framefmt *mf)
  40. {
  41. int i;
  42. for (i = 0; i < ARRAY_SIZE(fimc_is_sensor_formats); i++)
  43. if (mf->code == fimc_is_sensor_formats[i].code)
  44. return &fimc_is_sensor_formats[i];
  45. return &fimc_is_sensor_formats[0];
  46. }
  47. static int fimc_is_sensor_enum_mbus_code(struct v4l2_subdev *sd,
  48. struct v4l2_subdev_fh *fh,
  49. struct v4l2_subdev_mbus_code_enum *code)
  50. {
  51. if (code->index >= ARRAY_SIZE(fimc_is_sensor_formats))
  52. return -EINVAL;
  53. code->code = fimc_is_sensor_formats[code->index].code;
  54. return 0;
  55. }
  56. static void fimc_is_sensor_try_format(struct fimc_is_sensor *sensor,
  57. struct v4l2_mbus_framefmt *mf)
  58. {
  59. const struct sensor_drv_data *dd = sensor->drvdata;
  60. const struct v4l2_mbus_framefmt *fmt;
  61. fmt = find_sensor_format(mf);
  62. mf->code = fmt->code;
  63. v4l_bound_align_image(&mf->width, 16 + 8, dd->width, 0,
  64. &mf->height, 12 + 8, dd->height, 0, 0);
  65. }
  66. static struct v4l2_mbus_framefmt *__fimc_is_sensor_get_format(
  67. struct fimc_is_sensor *sensor, struct v4l2_subdev_fh *fh,
  68. u32 pad, enum v4l2_subdev_format_whence which)
  69. {
  70. if (which == V4L2_SUBDEV_FORMAT_TRY)
  71. return fh ? v4l2_subdev_get_try_format(fh, pad) : NULL;
  72. return &sensor->format;
  73. }
  74. static int fimc_is_sensor_set_fmt(struct v4l2_subdev *sd,
  75. struct v4l2_subdev_fh *fh,
  76. struct v4l2_subdev_format *fmt)
  77. {
  78. struct fimc_is_sensor *sensor = sd_to_fimc_is_sensor(sd);
  79. struct v4l2_mbus_framefmt *mf;
  80. fimc_is_sensor_try_format(sensor, &fmt->format);
  81. mf = __fimc_is_sensor_get_format(sensor, fh, fmt->pad, fmt->which);
  82. if (mf) {
  83. mutex_lock(&sensor->lock);
  84. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  85. *mf = fmt->format;
  86. mutex_unlock(&sensor->lock);
  87. }
  88. return 0;
  89. }
  90. static int fimc_is_sensor_get_fmt(struct v4l2_subdev *sd,
  91. struct v4l2_subdev_fh *fh,
  92. struct v4l2_subdev_format *fmt)
  93. {
  94. struct fimc_is_sensor *sensor = sd_to_fimc_is_sensor(sd);
  95. struct v4l2_mbus_framefmt *mf;
  96. mf = __fimc_is_sensor_get_format(sensor, fh, fmt->pad, fmt->which);
  97. mutex_lock(&sensor->lock);
  98. fmt->format = *mf;
  99. mutex_unlock(&sensor->lock);
  100. return 0;
  101. }
  102. static struct v4l2_subdev_pad_ops fimc_is_sensor_pad_ops = {
  103. .enum_mbus_code = fimc_is_sensor_enum_mbus_code,
  104. .get_fmt = fimc_is_sensor_get_fmt,
  105. .set_fmt = fimc_is_sensor_set_fmt,
  106. };
  107. static int fimc_is_sensor_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  108. {
  109. struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(fh, 0);
  110. *format = fimc_is_sensor_formats[0];
  111. format->width = FIMC_IS_SENSOR_DEF_PIX_WIDTH;
  112. format->height = FIMC_IS_SENSOR_DEF_PIX_HEIGHT;
  113. return 0;
  114. }
  115. static const struct v4l2_subdev_internal_ops fimc_is_sensor_sd_internal_ops = {
  116. .open = fimc_is_sensor_open,
  117. };
  118. static int fimc_is_sensor_s_power(struct v4l2_subdev *sd, int on)
  119. {
  120. struct fimc_is_sensor *sensor = sd_to_fimc_is_sensor(sd);
  121. int gpio = sensor->gpio_reset;
  122. int ret;
  123. if (on) {
  124. ret = pm_runtime_get(sensor->dev);
  125. if (ret < 0)
  126. return ret;
  127. ret = regulator_bulk_enable(SENSOR_NUM_SUPPLIES,
  128. sensor->supplies);
  129. if (ret < 0) {
  130. pm_runtime_put(sensor->dev);
  131. return ret;
  132. }
  133. if (gpio_is_valid(gpio)) {
  134. gpio_set_value(gpio, 1);
  135. usleep_range(600, 800);
  136. gpio_set_value(gpio, 0);
  137. usleep_range(10000, 11000);
  138. gpio_set_value(gpio, 1);
  139. }
  140. /* A delay needed for the sensor initialization. */
  141. msleep(20);
  142. } else {
  143. if (gpio_is_valid(gpio))
  144. gpio_set_value(gpio, 0);
  145. ret = regulator_bulk_disable(SENSOR_NUM_SUPPLIES,
  146. sensor->supplies);
  147. if (!ret)
  148. pm_runtime_put(sensor->dev);
  149. }
  150. pr_info("%s:%d: on: %d, ret: %d\n", __func__, __LINE__, on, ret);
  151. return ret;
  152. }
  153. static struct v4l2_subdev_core_ops fimc_is_sensor_core_ops = {
  154. .s_power = fimc_is_sensor_s_power,
  155. };
  156. static struct v4l2_subdev_ops fimc_is_sensor_subdev_ops = {
  157. .core = &fimc_is_sensor_core_ops,
  158. .pad = &fimc_is_sensor_pad_ops,
  159. };
  160. static const struct of_device_id fimc_is_sensor_of_match[];
  161. static int fimc_is_sensor_probe(struct i2c_client *client,
  162. const struct i2c_device_id *id)
  163. {
  164. struct device *dev = &client->dev;
  165. struct fimc_is_sensor *sensor;
  166. const struct of_device_id *of_id;
  167. struct v4l2_subdev *sd;
  168. int gpio, i, ret;
  169. sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
  170. if (!sensor)
  171. return -ENOMEM;
  172. mutex_init(&sensor->lock);
  173. sensor->gpio_reset = -EINVAL;
  174. gpio = of_get_gpio_flags(dev->of_node, 0, NULL);
  175. if (gpio_is_valid(gpio)) {
  176. ret = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_LOW,
  177. DRIVER_NAME);
  178. if (ret < 0)
  179. return ret;
  180. }
  181. sensor->gpio_reset = gpio;
  182. for (i = 0; i < SENSOR_NUM_SUPPLIES; i++)
  183. sensor->supplies[i].supply = sensor_supply_names[i];
  184. ret = devm_regulator_bulk_get(&client->dev, SENSOR_NUM_SUPPLIES,
  185. sensor->supplies);
  186. if (ret < 0)
  187. return ret;
  188. of_id = of_match_node(fimc_is_sensor_of_match, dev->of_node);
  189. if (!of_id)
  190. return -ENODEV;
  191. sensor->drvdata = of_id->data;
  192. sensor->dev = dev;
  193. sd = &sensor->subdev;
  194. v4l2_i2c_subdev_init(sd, client, &fimc_is_sensor_subdev_ops);
  195. snprintf(sd->name, sizeof(sd->name), sensor->drvdata->subdev_name);
  196. sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  197. sensor->format.code = fimc_is_sensor_formats[0].code;
  198. sensor->format.width = FIMC_IS_SENSOR_DEF_PIX_WIDTH;
  199. sensor->format.height = FIMC_IS_SENSOR_DEF_PIX_HEIGHT;
  200. sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
  201. ret = media_entity_init(&sd->entity, 1, &sensor->pad, 0);
  202. if (ret < 0)
  203. return ret;
  204. pm_runtime_no_callbacks(dev);
  205. pm_runtime_enable(dev);
  206. return ret;
  207. }
  208. static int fimc_is_sensor_remove(struct i2c_client *client)
  209. {
  210. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  211. media_entity_cleanup(&sd->entity);
  212. return 0;
  213. }
  214. static const struct i2c_device_id fimc_is_sensor_ids[] = {
  215. { }
  216. };
  217. static const struct sensor_drv_data s5k6a3_drvdata = {
  218. .id = FIMC_IS_SENSOR_ID_S5K6A3,
  219. .subdev_name = "S5K6A3",
  220. .width = S5K6A3_SENSOR_WIDTH,
  221. .height = S5K6A3_SENSOR_HEIGHT,
  222. };
  223. static const struct of_device_id fimc_is_sensor_of_match[] = {
  224. {
  225. .compatible = "samsung,s5k6a3",
  226. .data = &s5k6a3_drvdata,
  227. },
  228. { }
  229. };
  230. static struct i2c_driver fimc_is_sensor_driver = {
  231. .driver = {
  232. .of_match_table = fimc_is_sensor_of_match,
  233. .name = DRIVER_NAME,
  234. .owner = THIS_MODULE,
  235. },
  236. .probe = fimc_is_sensor_probe,
  237. .remove = fimc_is_sensor_remove,
  238. .id_table = fimc_is_sensor_ids,
  239. };
  240. int fimc_is_register_sensor_driver(void)
  241. {
  242. return i2c_add_driver(&fimc_is_sensor_driver);
  243. }
  244. void fimc_is_unregister_sensor_driver(void)
  245. {
  246. i2c_del_driver(&fimc_is_sensor_driver);
  247. }
  248. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  249. MODULE_DESCRIPTION("Exynos4x12 FIMC-IS image sensor subdev driver");
  250. MODULE_LICENSE("GPL");