s5k6a3.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Samsung S5K6A3 image sensor driver
  4. *
  5. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  6. * Author: Sylwester Nawrocki <[email protected]>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/errno.h>
  12. #include <linux/gpio.h>
  13. #include <linux/i2c.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of_gpio.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/slab.h>
  20. #include <linux/videodev2.h>
  21. #include <media/v4l2-async.h>
  22. #include <media/v4l2-subdev.h>
  23. #define S5K6A3_SENSOR_MAX_WIDTH 1412
  24. #define S5K6A3_SENSOR_MAX_HEIGHT 1412
  25. #define S5K6A3_SENSOR_MIN_WIDTH 32
  26. #define S5K6A3_SENSOR_MIN_HEIGHT 32
  27. #define S5K6A3_DEFAULT_WIDTH 1296
  28. #define S5K6A3_DEFAULT_HEIGHT 732
  29. #define S5K6A3_DRV_NAME "S5K6A3"
  30. #define S5K6A3_CLK_NAME "extclk"
  31. #define S5K6A3_DEFAULT_CLK_FREQ 24000000U
  32. enum {
  33. S5K6A3_SUPP_VDDA,
  34. S5K6A3_SUPP_VDDIO,
  35. S5K6A3_SUPP_AFVDD,
  36. S5K6A3_NUM_SUPPLIES,
  37. };
  38. /**
  39. * struct s5k6a3 - fimc-is sensor data structure
  40. * @dev: pointer to this I2C client device structure
  41. * @subdev: the image sensor's v4l2 subdev
  42. * @pad: subdev media source pad
  43. * @supplies: image sensor's voltage regulator supplies
  44. * @gpio_reset: GPIO connected to the sensor's reset pin
  45. * @lock: mutex protecting the structure's members below
  46. * @format: media bus format at the sensor's source pad
  47. * @clock: pointer to &struct clk.
  48. * @clock_frequency: clock frequency
  49. * @power_count: stores state if device is powered
  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_state *sd_state,
  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_state *sd_state,
  110. u32 pad, enum v4l2_subdev_format_whence which)
  111. {
  112. if (which == V4L2_SUBDEV_FORMAT_TRY)
  113. return sd_state ? v4l2_subdev_get_try_format(&sensor->subdev,
  114. sd_state, pad) : NULL;
  115. return &sensor->format;
  116. }
  117. static int s5k6a3_set_fmt(struct v4l2_subdev *sd,
  118. struct v4l2_subdev_state *sd_state,
  119. struct v4l2_subdev_format *fmt)
  120. {
  121. struct s5k6a3 *sensor = sd_to_s5k6a3(sd);
  122. struct v4l2_mbus_framefmt *mf;
  123. s5k6a3_try_format(&fmt->format);
  124. mf = __s5k6a3_get_format(sensor, sd_state, fmt->pad, fmt->which);
  125. if (mf) {
  126. mutex_lock(&sensor->lock);
  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_state *sd_state,
  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, sd_state, fmt->pad, fmt->which);
  139. mutex_lock(&sensor->lock);
  140. fmt->format = *mf;
  141. mutex_unlock(&sensor->lock);
  142. return 0;
  143. }
  144. static const 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,
  152. fh->state,
  153. 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. goto error_rpm_put;
  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_clk;
  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_clk:
  192. clk_disable_unprepare(sensor->clock);
  193. error_reg_dis:
  194. for (--i; i >= 0; --i)
  195. regulator_disable(sensor->supplies[i].consumer);
  196. error_rpm_put:
  197. pm_runtime_put(sensor->dev);
  198. return ret;
  199. }
  200. static int __s5k6a3_power_off(struct s5k6a3 *sensor)
  201. {
  202. int i;
  203. gpio_set_value(sensor->gpio_reset, 0);
  204. for (i = S5K6A3_NUM_SUPPLIES - 1; i >= 0; i--)
  205. regulator_disable(sensor->supplies[i].consumer);
  206. clk_disable_unprepare(sensor->clock);
  207. pm_runtime_put(sensor->dev);
  208. return 0;
  209. }
  210. static int s5k6a3_s_power(struct v4l2_subdev *sd, int on)
  211. {
  212. struct s5k6a3 *sensor = sd_to_s5k6a3(sd);
  213. int ret = 0;
  214. mutex_lock(&sensor->lock);
  215. if (sensor->power_count == !on) {
  216. if (on)
  217. ret = __s5k6a3_power_on(sensor);
  218. else
  219. ret = __s5k6a3_power_off(sensor);
  220. if (ret == 0)
  221. sensor->power_count += on ? 1 : -1;
  222. }
  223. mutex_unlock(&sensor->lock);
  224. return ret;
  225. }
  226. static const struct v4l2_subdev_core_ops s5k6a3_core_ops = {
  227. .s_power = s5k6a3_s_power,
  228. };
  229. static const struct v4l2_subdev_ops s5k6a3_subdev_ops = {
  230. .core = &s5k6a3_core_ops,
  231. .pad = &s5k6a3_pad_ops,
  232. };
  233. static int s5k6a3_probe(struct i2c_client *client)
  234. {
  235. struct device *dev = &client->dev;
  236. struct s5k6a3 *sensor;
  237. struct v4l2_subdev *sd;
  238. int gpio, i, ret;
  239. sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
  240. if (!sensor)
  241. return -ENOMEM;
  242. mutex_init(&sensor->lock);
  243. sensor->gpio_reset = -EINVAL;
  244. sensor->clock = ERR_PTR(-EINVAL);
  245. sensor->dev = dev;
  246. sensor->clock = devm_clk_get(sensor->dev, S5K6A3_CLK_NAME);
  247. if (IS_ERR(sensor->clock))
  248. return PTR_ERR(sensor->clock);
  249. gpio = of_get_gpio_flags(dev->of_node, 0, NULL);
  250. if (!gpio_is_valid(gpio))
  251. return gpio;
  252. ret = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_LOW,
  253. S5K6A3_DRV_NAME);
  254. if (ret < 0)
  255. return ret;
  256. sensor->gpio_reset = gpio;
  257. if (of_property_read_u32(dev->of_node, "clock-frequency",
  258. &sensor->clock_frequency)) {
  259. sensor->clock_frequency = S5K6A3_DEFAULT_CLK_FREQ;
  260. dev_info(dev, "using default %u Hz clock frequency\n",
  261. sensor->clock_frequency);
  262. }
  263. for (i = 0; i < S5K6A3_NUM_SUPPLIES; i++)
  264. sensor->supplies[i].supply = s5k6a3_supply_names[i];
  265. ret = devm_regulator_bulk_get(&client->dev, S5K6A3_NUM_SUPPLIES,
  266. sensor->supplies);
  267. if (ret < 0)
  268. return ret;
  269. sd = &sensor->subdev;
  270. v4l2_i2c_subdev_init(sd, client, &s5k6a3_subdev_ops);
  271. sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  272. sd->internal_ops = &s5k6a3_sd_internal_ops;
  273. sensor->format.code = s5k6a3_formats[0].code;
  274. sensor->format.width = S5K6A3_DEFAULT_WIDTH;
  275. sensor->format.height = S5K6A3_DEFAULT_HEIGHT;
  276. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  277. sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
  278. ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad);
  279. if (ret < 0)
  280. return ret;
  281. pm_runtime_no_callbacks(dev);
  282. pm_runtime_enable(dev);
  283. ret = v4l2_async_register_subdev(sd);
  284. if (ret < 0) {
  285. pm_runtime_disable(&client->dev);
  286. media_entity_cleanup(&sd->entity);
  287. }
  288. return ret;
  289. }
  290. static void s5k6a3_remove(struct i2c_client *client)
  291. {
  292. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  293. pm_runtime_disable(&client->dev);
  294. v4l2_async_unregister_subdev(sd);
  295. media_entity_cleanup(&sd->entity);
  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_new = 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 <[email protected]>");
  320. MODULE_LICENSE("GPL v2");