s5k6a3.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. *mf = fmt->format;
  127. mutex_unlock(&sensor->lock);
  128. }
  129. return 0;
  130. }
  131. static int s5k6a3_get_fmt(struct v4l2_subdev *sd,
  132. struct v4l2_subdev_pad_config *cfg,
  133. struct v4l2_subdev_format *fmt)
  134. {
  135. struct s5k6a3 *sensor = sd_to_s5k6a3(sd);
  136. struct v4l2_mbus_framefmt *mf;
  137. mf = __s5k6a3_get_format(sensor, cfg, fmt->pad, fmt->which);
  138. mutex_lock(&sensor->lock);
  139. fmt->format = *mf;
  140. mutex_unlock(&sensor->lock);
  141. return 0;
  142. }
  143. static struct v4l2_subdev_pad_ops s5k6a3_pad_ops = {
  144. .enum_mbus_code = s5k6a3_enum_mbus_code,
  145. .get_fmt = s5k6a3_get_fmt,
  146. .set_fmt = s5k6a3_set_fmt,
  147. };
  148. static int s5k6a3_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  149. {
  150. struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(sd, fh->pad, 0);
  151. *format = s5k6a3_formats[0];
  152. format->width = S5K6A3_DEFAULT_WIDTH;
  153. format->height = S5K6A3_DEFAULT_HEIGHT;
  154. return 0;
  155. }
  156. static const struct v4l2_subdev_internal_ops s5k6a3_sd_internal_ops = {
  157. .open = s5k6a3_open,
  158. };
  159. static int __s5k6a3_power_on(struct s5k6a3 *sensor)
  160. {
  161. int i = S5K6A3_SUPP_VDDA;
  162. int ret;
  163. ret = clk_set_rate(sensor->clock, sensor->clock_frequency);
  164. if (ret < 0)
  165. return ret;
  166. ret = pm_runtime_get(sensor->dev);
  167. if (ret < 0)
  168. return ret;
  169. ret = regulator_enable(sensor->supplies[i].consumer);
  170. if (ret < 0)
  171. goto error_rpm_put;
  172. ret = clk_prepare_enable(sensor->clock);
  173. if (ret < 0)
  174. goto error_reg_dis;
  175. for (i++; i < S5K6A3_NUM_SUPPLIES; i++) {
  176. ret = regulator_enable(sensor->supplies[i].consumer);
  177. if (ret < 0)
  178. goto error_reg_dis;
  179. }
  180. gpio_set_value(sensor->gpio_reset, 1);
  181. usleep_range(600, 800);
  182. gpio_set_value(sensor->gpio_reset, 0);
  183. usleep_range(600, 800);
  184. gpio_set_value(sensor->gpio_reset, 1);
  185. /* Delay needed for the sensor initialization */
  186. msleep(20);
  187. return 0;
  188. error_reg_dis:
  189. for (--i; i >= 0; --i)
  190. regulator_disable(sensor->supplies[i].consumer);
  191. error_rpm_put:
  192. pm_runtime_put(sensor->dev);
  193. return ret;
  194. }
  195. static int __s5k6a3_power_off(struct s5k6a3 *sensor)
  196. {
  197. int i;
  198. gpio_set_value(sensor->gpio_reset, 0);
  199. for (i = S5K6A3_NUM_SUPPLIES - 1; i >= 0; i--)
  200. regulator_disable(sensor->supplies[i].consumer);
  201. clk_disable_unprepare(sensor->clock);
  202. pm_runtime_put(sensor->dev);
  203. return 0;
  204. }
  205. static int s5k6a3_s_power(struct v4l2_subdev *sd, int on)
  206. {
  207. struct s5k6a3 *sensor = sd_to_s5k6a3(sd);
  208. int ret = 0;
  209. mutex_lock(&sensor->lock);
  210. if (sensor->power_count == !on) {
  211. if (on)
  212. ret = __s5k6a3_power_on(sensor);
  213. else
  214. ret = __s5k6a3_power_off(sensor);
  215. if (ret == 0)
  216. sensor->power_count += on ? 1 : -1;
  217. }
  218. mutex_unlock(&sensor->lock);
  219. return ret;
  220. }
  221. static struct v4l2_subdev_core_ops s5k6a3_core_ops = {
  222. .s_power = s5k6a3_s_power,
  223. };
  224. static struct v4l2_subdev_ops s5k6a3_subdev_ops = {
  225. .core = &s5k6a3_core_ops,
  226. .pad = &s5k6a3_pad_ops,
  227. };
  228. static int s5k6a3_probe(struct i2c_client *client,
  229. const struct i2c_device_id *id)
  230. {
  231. struct device *dev = &client->dev;
  232. struct s5k6a3 *sensor;
  233. struct v4l2_subdev *sd;
  234. int gpio, i, ret;
  235. sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
  236. if (!sensor)
  237. return -ENOMEM;
  238. mutex_init(&sensor->lock);
  239. sensor->gpio_reset = -EINVAL;
  240. sensor->clock = ERR_PTR(-EINVAL);
  241. sensor->dev = dev;
  242. sensor->clock = devm_clk_get(sensor->dev, S5K6A3_CLK_NAME);
  243. if (IS_ERR(sensor->clock))
  244. return PTR_ERR(sensor->clock);
  245. gpio = of_get_gpio_flags(dev->of_node, 0, NULL);
  246. if (!gpio_is_valid(gpio))
  247. return gpio;
  248. ret = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_LOW,
  249. S5K6A3_DRV_NAME);
  250. if (ret < 0)
  251. return ret;
  252. sensor->gpio_reset = gpio;
  253. if (of_property_read_u32(dev->of_node, "clock-frequency",
  254. &sensor->clock_frequency)) {
  255. sensor->clock_frequency = S5K6A3_DEFAULT_CLK_FREQ;
  256. dev_info(dev, "using default %u Hz clock frequency\n",
  257. sensor->clock_frequency);
  258. }
  259. for (i = 0; i < S5K6A3_NUM_SUPPLIES; i++)
  260. sensor->supplies[i].supply = s5k6a3_supply_names[i];
  261. ret = devm_regulator_bulk_get(&client->dev, S5K6A3_NUM_SUPPLIES,
  262. sensor->supplies);
  263. if (ret < 0)
  264. return ret;
  265. sd = &sensor->subdev;
  266. v4l2_i2c_subdev_init(sd, client, &s5k6a3_subdev_ops);
  267. sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  268. sd->internal_ops = &s5k6a3_sd_internal_ops;
  269. sensor->format.code = s5k6a3_formats[0].code;
  270. sensor->format.width = S5K6A3_DEFAULT_WIDTH;
  271. sensor->format.height = S5K6A3_DEFAULT_HEIGHT;
  272. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  273. sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
  274. ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad);
  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. },
  310. .probe = s5k6a3_probe,
  311. .remove = s5k6a3_remove,
  312. .id_table = s5k6a3_ids,
  313. };
  314. module_i2c_driver(s5k6a3_driver);
  315. MODULE_DESCRIPTION("S5K6A3 image sensor subdev driver");
  316. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  317. MODULE_LICENSE("GPL v2");