s5k6a3.c 9.4 KB

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