s5k6a3.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Samsung S5K6A3 image sensor driver
  3. *
  4. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  5. * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/clk.h>
  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 <linux/videodev2.h>
  24. #include <media/v4l2-async.h>
  25. #include <media/v4l2-subdev.h>
  26. #define S5K6A3_SENSOR_MAX_WIDTH 1412
  27. #define S5K6A3_SENSOR_MAX_HEIGHT 1412
  28. #define S5K6A3_SENSOR_MIN_WIDTH 32
  29. #define S5K6A3_SENSOR_MIN_HEIGHT 32
  30. #define S5K6A3_DEFAULT_WIDTH 1296
  31. #define S5K6A3_DEFAULT_HEIGHT 732
  32. #define S5K6A3_DRV_NAME "S5K6A3"
  33. #define S5K6A3_CLK_NAME "extclk"
  34. #define S5K6A3_DEFAULT_CLK_FREQ 24000000U
  35. enum {
  36. S5K6A3_SUPP_VDDA,
  37. S5K6A3_SUPP_VDDIO,
  38. S5K6A3_SUPP_AFVDD,
  39. S5K6A3_NUM_SUPPLIES,
  40. };
  41. /**
  42. * struct s5k6a3 - fimc-is sensor data structure
  43. * @dev: pointer to this I2C client device structure
  44. * @subdev: the image sensor's v4l2 subdev
  45. * @pad: subdev media source pad
  46. * @supplies: image sensor's voltage regulator supplies
  47. * @gpio_reset: GPIO connected to the sensor's reset pin
  48. * @lock: mutex protecting the structure's members below
  49. * @format: media bus format at the sensor's source pad
  50. */
  51. struct s5k6a3 {
  52. struct device *dev;
  53. struct v4l2_subdev subdev;
  54. struct media_pad pad;
  55. struct regulator_bulk_data supplies[S5K6A3_NUM_SUPPLIES];
  56. int gpio_reset;
  57. struct mutex lock;
  58. struct v4l2_mbus_framefmt format;
  59. struct clk *clock;
  60. u32 clock_frequency;
  61. int power_count;
  62. };
  63. static const char * const s5k6a3_supply_names[] = {
  64. [S5K6A3_SUPP_VDDA] = "svdda",
  65. [S5K6A3_SUPP_VDDIO] = "svddio",
  66. [S5K6A3_SUPP_AFVDD] = "afvdd",
  67. };
  68. static inline struct s5k6a3 *sd_to_s5k6a3(struct v4l2_subdev *sd)
  69. {
  70. return container_of(sd, struct s5k6a3, subdev);
  71. }
  72. static const struct v4l2_mbus_framefmt s5k6a3_formats[] = {
  73. {
  74. .code = MEDIA_BUS_FMT_SGRBG10_1X10,
  75. .colorspace = V4L2_COLORSPACE_SRGB,
  76. .field = V4L2_FIELD_NONE,
  77. }
  78. };
  79. static const struct v4l2_mbus_framefmt *find_sensor_format(
  80. struct v4l2_mbus_framefmt *mf)
  81. {
  82. int i;
  83. for (i = 0; i < ARRAY_SIZE(s5k6a3_formats); i++)
  84. if (mf->code == s5k6a3_formats[i].code)
  85. return &s5k6a3_formats[i];
  86. return &s5k6a3_formats[0];
  87. }
  88. static int s5k6a3_enum_mbus_code(struct v4l2_subdev *sd,
  89. struct v4l2_subdev_pad_config *cfg,
  90. struct v4l2_subdev_mbus_code_enum *code)
  91. {
  92. if (code->index >= ARRAY_SIZE(s5k6a3_formats))
  93. return -EINVAL;
  94. code->code = s5k6a3_formats[code->index].code;
  95. return 0;
  96. }
  97. static void s5k6a3_try_format(struct v4l2_mbus_framefmt *mf)
  98. {
  99. const struct v4l2_mbus_framefmt *fmt;
  100. fmt = find_sensor_format(mf);
  101. mf->code = fmt->code;
  102. mf->field = V4L2_FIELD_NONE;
  103. v4l_bound_align_image(&mf->width, S5K6A3_SENSOR_MIN_WIDTH,
  104. S5K6A3_SENSOR_MAX_WIDTH, 0,
  105. &mf->height, S5K6A3_SENSOR_MIN_HEIGHT,
  106. S5K6A3_SENSOR_MAX_HEIGHT, 0, 0);
  107. }
  108. static struct v4l2_mbus_framefmt *__s5k6a3_get_format(
  109. struct s5k6a3 *sensor, struct v4l2_subdev_pad_config *cfg,
  110. u32 pad, enum v4l2_subdev_format_whence which)
  111. {
  112. if (which == V4L2_SUBDEV_FORMAT_TRY)
  113. return cfg ? v4l2_subdev_get_try_format(&sensor->subdev, cfg, pad) : NULL;
  114. return &sensor->format;
  115. }
  116. static int s5k6a3_set_fmt(struct v4l2_subdev *sd,
  117. struct v4l2_subdev_pad_config *cfg,
  118. struct v4l2_subdev_format *fmt)
  119. {
  120. struct s5k6a3 *sensor = sd_to_s5k6a3(sd);
  121. struct v4l2_mbus_framefmt *mf;
  122. s5k6a3_try_format(&fmt->format);
  123. mf = __s5k6a3_get_format(sensor, cfg, fmt->pad, fmt->which);
  124. if (mf) {
  125. mutex_lock(&sensor->lock);
  126. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  127. *mf = fmt->format;
  128. mutex_unlock(&sensor->lock);
  129. }
  130. return 0;
  131. }
  132. static int s5k6a3_get_fmt(struct v4l2_subdev *sd,
  133. struct v4l2_subdev_pad_config *cfg,
  134. struct v4l2_subdev_format *fmt)
  135. {
  136. struct s5k6a3 *sensor = sd_to_s5k6a3(sd);
  137. struct v4l2_mbus_framefmt *mf;
  138. mf = __s5k6a3_get_format(sensor, cfg, fmt->pad, fmt->which);
  139. mutex_lock(&sensor->lock);
  140. fmt->format = *mf;
  141. mutex_unlock(&sensor->lock);
  142. return 0;
  143. }
  144. static struct v4l2_subdev_pad_ops s5k6a3_pad_ops = {
  145. .enum_mbus_code = s5k6a3_enum_mbus_code,
  146. .get_fmt = s5k6a3_get_fmt,
  147. .set_fmt = s5k6a3_set_fmt,
  148. };
  149. static int s5k6a3_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  150. {
  151. struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(sd, fh->pad, 0);
  152. *format = s5k6a3_formats[0];
  153. format->width = S5K6A3_DEFAULT_WIDTH;
  154. format->height = S5K6A3_DEFAULT_HEIGHT;
  155. return 0;
  156. }
  157. static const struct v4l2_subdev_internal_ops s5k6a3_sd_internal_ops = {
  158. .open = s5k6a3_open,
  159. };
  160. static int __s5k6a3_power_on(struct s5k6a3 *sensor)
  161. {
  162. int i = S5K6A3_SUPP_VDDA;
  163. int ret;
  164. ret = clk_set_rate(sensor->clock, sensor->clock_frequency);
  165. if (ret < 0)
  166. return ret;
  167. ret = pm_runtime_get(sensor->dev);
  168. if (ret < 0)
  169. return ret;
  170. ret = regulator_enable(sensor->supplies[i].consumer);
  171. if (ret < 0)
  172. goto error_rpm_put;
  173. ret = clk_prepare_enable(sensor->clock);
  174. if (ret < 0)
  175. goto error_reg_dis;
  176. for (i++; i < S5K6A3_NUM_SUPPLIES; i++) {
  177. ret = regulator_enable(sensor->supplies[i].consumer);
  178. if (ret < 0)
  179. goto error_reg_dis;
  180. }
  181. gpio_set_value(sensor->gpio_reset, 1);
  182. usleep_range(600, 800);
  183. gpio_set_value(sensor->gpio_reset, 0);
  184. usleep_range(600, 800);
  185. gpio_set_value(sensor->gpio_reset, 1);
  186. /* Delay needed for the sensor initialization */
  187. msleep(20);
  188. return 0;
  189. error_reg_dis:
  190. for (--i; i >= 0; --i)
  191. regulator_disable(sensor->supplies[i].consumer);
  192. error_rpm_put:
  193. pm_runtime_put(sensor->dev);
  194. return ret;
  195. }
  196. static int __s5k6a3_power_off(struct s5k6a3 *sensor)
  197. {
  198. int i;
  199. gpio_set_value(sensor->gpio_reset, 0);
  200. for (i = S5K6A3_NUM_SUPPLIES - 1; i >= 0; i--)
  201. regulator_disable(sensor->supplies[i].consumer);
  202. clk_disable_unprepare(sensor->clock);
  203. pm_runtime_put(sensor->dev);
  204. return 0;
  205. }
  206. static int s5k6a3_s_power(struct v4l2_subdev *sd, int on)
  207. {
  208. struct s5k6a3 *sensor = sd_to_s5k6a3(sd);
  209. int ret = 0;
  210. mutex_lock(&sensor->lock);
  211. if (sensor->power_count == !on) {
  212. if (on)
  213. ret = __s5k6a3_power_on(sensor);
  214. else
  215. ret = __s5k6a3_power_off(sensor);
  216. if (ret == 0)
  217. sensor->power_count += on ? 1 : -1;
  218. }
  219. mutex_unlock(&sensor->lock);
  220. return ret;
  221. }
  222. static struct v4l2_subdev_core_ops s5k6a3_core_ops = {
  223. .s_power = s5k6a3_s_power,
  224. };
  225. static struct v4l2_subdev_ops s5k6a3_subdev_ops = {
  226. .core = &s5k6a3_core_ops,
  227. .pad = &s5k6a3_pad_ops,
  228. };
  229. static int s5k6a3_probe(struct i2c_client *client,
  230. const struct i2c_device_id *id)
  231. {
  232. struct device *dev = &client->dev;
  233. struct s5k6a3 *sensor;
  234. struct v4l2_subdev *sd;
  235. int gpio, i, ret;
  236. sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
  237. if (!sensor)
  238. return -ENOMEM;
  239. mutex_init(&sensor->lock);
  240. sensor->gpio_reset = -EINVAL;
  241. sensor->clock = ERR_PTR(-EINVAL);
  242. sensor->dev = dev;
  243. sensor->clock = devm_clk_get(sensor->dev, S5K6A3_CLK_NAME);
  244. if (IS_ERR(sensor->clock))
  245. return PTR_ERR(sensor->clock);
  246. gpio = of_get_gpio_flags(dev->of_node, 0, NULL);
  247. if (!gpio_is_valid(gpio))
  248. return gpio;
  249. ret = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_LOW,
  250. S5K6A3_DRV_NAME);
  251. if (ret < 0)
  252. return ret;
  253. sensor->gpio_reset = gpio;
  254. if (of_property_read_u32(dev->of_node, "clock-frequency",
  255. &sensor->clock_frequency)) {
  256. sensor->clock_frequency = S5K6A3_DEFAULT_CLK_FREQ;
  257. dev_info(dev, "using default %u Hz clock frequency\n",
  258. sensor->clock_frequency);
  259. }
  260. for (i = 0; i < S5K6A3_NUM_SUPPLIES; i++)
  261. sensor->supplies[i].supply = s5k6a3_supply_names[i];
  262. ret = devm_regulator_bulk_get(&client->dev, S5K6A3_NUM_SUPPLIES,
  263. sensor->supplies);
  264. if (ret < 0)
  265. return ret;
  266. sd = &sensor->subdev;
  267. v4l2_i2c_subdev_init(sd, client, &s5k6a3_subdev_ops);
  268. sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  269. sd->internal_ops = &s5k6a3_sd_internal_ops;
  270. sensor->format.code = s5k6a3_formats[0].code;
  271. sensor->format.width = S5K6A3_DEFAULT_WIDTH;
  272. sensor->format.height = S5K6A3_DEFAULT_HEIGHT;
  273. sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
  274. ret = media_entity_init(&sd->entity, 1, &sensor->pad, 0);
  275. if (ret < 0)
  276. return ret;
  277. pm_runtime_no_callbacks(dev);
  278. pm_runtime_enable(dev);
  279. ret = v4l2_async_register_subdev(sd);
  280. if (ret < 0) {
  281. pm_runtime_disable(&client->dev);
  282. media_entity_cleanup(&sd->entity);
  283. }
  284. return ret;
  285. }
  286. static int s5k6a3_remove(struct i2c_client *client)
  287. {
  288. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  289. pm_runtime_disable(&client->dev);
  290. v4l2_async_unregister_subdev(sd);
  291. media_entity_cleanup(&sd->entity);
  292. return 0;
  293. }
  294. static const struct i2c_device_id s5k6a3_ids[] = {
  295. { }
  296. };
  297. MODULE_DEVICE_TABLE(i2c, s5k6a3_ids);
  298. #ifdef CONFIG_OF
  299. static const struct of_device_id s5k6a3_of_match[] = {
  300. { .compatible = "samsung,s5k6a3" },
  301. { /* sentinel */ }
  302. };
  303. MODULE_DEVICE_TABLE(of, s5k6a3_of_match);
  304. #endif
  305. static struct i2c_driver s5k6a3_driver = {
  306. .driver = {
  307. .of_match_table = of_match_ptr(s5k6a3_of_match),
  308. .name = S5K6A3_DRV_NAME,
  309. .owner = THIS_MODULE,
  310. },
  311. .probe = s5k6a3_probe,
  312. .remove = s5k6a3_remove,
  313. .id_table = s5k6a3_ids,
  314. };
  315. module_i2c_driver(s5k6a3_driver);
  316. MODULE_DESCRIPTION("S5K6A3 image sensor subdev driver");
  317. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  318. MODULE_LICENSE("GPL v2");