ov02a10.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2020 MediaTek Inc.
  3. #include <linux/clk.h>
  4. #include <linux/delay.h>
  5. #include <linux/device.h>
  6. #include <linux/gpio/consumer.h>
  7. #include <linux/i2c.h>
  8. #include <linux/module.h>
  9. #include <linux/pm_runtime.h>
  10. #include <linux/regulator/consumer.h>
  11. #include <linux/units.h>
  12. #include <media/media-entity.h>
  13. #include <media/v4l2-async.h>
  14. #include <media/v4l2-ctrls.h>
  15. #include <media/v4l2-fwnode.h>
  16. #include <media/v4l2-subdev.h>
  17. #define OV02A10_ID 0x2509
  18. #define OV02A10_ID_MASK GENMASK(15, 0)
  19. #define OV02A10_REG_CHIP_ID 0x02
  20. /* Bit[1] vertical upside down */
  21. /* Bit[0] horizontal mirror */
  22. #define REG_MIRROR_FLIP_CONTROL 0x3f
  23. /* Orientation */
  24. #define REG_MIRROR_FLIP_ENABLE 0x03
  25. /* Bit[2:0] MIPI transmission speed select */
  26. #define TX_SPEED_AREA_SEL 0xa1
  27. #define OV02A10_MIPI_TX_SPEED_DEFAULT 0x04
  28. #define REG_PAGE_SWITCH 0xfd
  29. #define REG_GLOBAL_EFFECTIVE 0x01
  30. #define REG_ENABLE BIT(0)
  31. #define REG_SC_CTRL_MODE 0xac
  32. #define SC_CTRL_MODE_STANDBY 0x00
  33. #define SC_CTRL_MODE_STREAMING 0x01
  34. /* Exposure control */
  35. #define OV02A10_EXP_SHIFT 8
  36. #define OV02A10_REG_EXPOSURE_H 0x03
  37. #define OV02A10_REG_EXPOSURE_L 0x04
  38. #define OV02A10_EXPOSURE_MIN 4
  39. #define OV02A10_EXPOSURE_MAX_MARGIN 4
  40. #define OV02A10_EXPOSURE_STEP 1
  41. /* Vblanking control */
  42. #define OV02A10_VTS_SHIFT 8
  43. #define OV02A10_REG_VTS_H 0x05
  44. #define OV02A10_REG_VTS_L 0x06
  45. #define OV02A10_VTS_MAX 0x209f
  46. #define OV02A10_BASE_LINES 1224
  47. /* Analog gain control */
  48. #define OV02A10_REG_GAIN 0x24
  49. #define OV02A10_GAIN_MIN 0x10
  50. #define OV02A10_GAIN_MAX 0xf8
  51. #define OV02A10_GAIN_STEP 0x01
  52. #define OV02A10_GAIN_DEFAULT 0x40
  53. /* Test pattern control */
  54. #define OV02A10_REG_TEST_PATTERN 0xb6
  55. #define OV02A10_LINK_FREQ_390MHZ (390 * HZ_PER_MHZ)
  56. #define OV02A10_ECLK_FREQ (24 * HZ_PER_MHZ)
  57. /* Number of lanes supported by this driver */
  58. #define OV02A10_DATA_LANES 1
  59. /* Bits per sample of sensor output */
  60. #define OV02A10_BITS_PER_SAMPLE 10
  61. static const char * const ov02a10_supply_names[] = {
  62. "dovdd", /* Digital I/O power */
  63. "avdd", /* Analog power */
  64. "dvdd", /* Digital core power */
  65. };
  66. struct ov02a10_reg {
  67. u8 addr;
  68. u8 val;
  69. };
  70. struct ov02a10_reg_list {
  71. u32 num_of_regs;
  72. const struct ov02a10_reg *regs;
  73. };
  74. struct ov02a10_mode {
  75. u32 width;
  76. u32 height;
  77. u32 exp_def;
  78. u32 hts_def;
  79. u32 vts_def;
  80. const struct ov02a10_reg_list reg_list;
  81. };
  82. struct ov02a10 {
  83. struct device *dev;
  84. /* Indication of MIPI transmission speed select */
  85. u32 mipi_clock_voltage;
  86. struct clk *eclk;
  87. struct gpio_desc *pd_gpio;
  88. struct gpio_desc *rst_gpio;
  89. struct regulator_bulk_data supplies[ARRAY_SIZE(ov02a10_supply_names)];
  90. bool streaming;
  91. bool upside_down;
  92. /*
  93. * Serialize control access, get/set format, get selection
  94. * and start streaming.
  95. */
  96. struct mutex mutex;
  97. struct v4l2_subdev subdev;
  98. struct media_pad pad;
  99. struct v4l2_mbus_framefmt fmt;
  100. struct v4l2_ctrl_handler ctrl_handler;
  101. struct v4l2_ctrl *exposure;
  102. const struct ov02a10_mode *cur_mode;
  103. };
  104. static inline struct ov02a10 *to_ov02a10(struct v4l2_subdev *sd)
  105. {
  106. return container_of(sd, struct ov02a10, subdev);
  107. }
  108. /*
  109. * eclk 24Mhz
  110. * pclk 39Mhz
  111. * linelength 934(0x3a6)
  112. * framelength 1390(0x56E)
  113. * grabwindow_width 1600
  114. * grabwindow_height 1200
  115. * max_framerate 30fps
  116. * mipi_datarate per lane 780Mbps
  117. */
  118. static const struct ov02a10_reg ov02a10_1600x1200_regs[] = {
  119. {0xfd, 0x01},
  120. {0xac, 0x00},
  121. {0xfd, 0x00},
  122. {0x2f, 0x29},
  123. {0x34, 0x00},
  124. {0x35, 0x21},
  125. {0x30, 0x15},
  126. {0x33, 0x01},
  127. {0xfd, 0x01},
  128. {0x44, 0x00},
  129. {0x2a, 0x4c},
  130. {0x2b, 0x1e},
  131. {0x2c, 0x60},
  132. {0x25, 0x11},
  133. {0x03, 0x01},
  134. {0x04, 0xae},
  135. {0x09, 0x00},
  136. {0x0a, 0x02},
  137. {0x06, 0xa6},
  138. {0x31, 0x00},
  139. {0x24, 0x40},
  140. {0x01, 0x01},
  141. {0xfb, 0x73},
  142. {0xfd, 0x01},
  143. {0x16, 0x04},
  144. {0x1c, 0x09},
  145. {0x21, 0x42},
  146. {0x12, 0x04},
  147. {0x13, 0x10},
  148. {0x11, 0x40},
  149. {0x33, 0x81},
  150. {0xd0, 0x00},
  151. {0xd1, 0x01},
  152. {0xd2, 0x00},
  153. {0x50, 0x10},
  154. {0x51, 0x23},
  155. {0x52, 0x20},
  156. {0x53, 0x10},
  157. {0x54, 0x02},
  158. {0x55, 0x20},
  159. {0x56, 0x02},
  160. {0x58, 0x48},
  161. {0x5d, 0x15},
  162. {0x5e, 0x05},
  163. {0x66, 0x66},
  164. {0x68, 0x68},
  165. {0x6b, 0x00},
  166. {0x6c, 0x00},
  167. {0x6f, 0x40},
  168. {0x70, 0x40},
  169. {0x71, 0x0a},
  170. {0x72, 0xf0},
  171. {0x73, 0x10},
  172. {0x75, 0x80},
  173. {0x76, 0x10},
  174. {0x84, 0x00},
  175. {0x85, 0x10},
  176. {0x86, 0x10},
  177. {0x87, 0x00},
  178. {0x8a, 0x22},
  179. {0x8b, 0x22},
  180. {0x19, 0xf1},
  181. {0x29, 0x01},
  182. {0xfd, 0x01},
  183. {0x9d, 0x16},
  184. {0xa0, 0x29},
  185. {0xa1, 0x04},
  186. {0xad, 0x62},
  187. {0xae, 0x00},
  188. {0xaf, 0x85},
  189. {0xb1, 0x01},
  190. {0x8e, 0x06},
  191. {0x8f, 0x40},
  192. {0x90, 0x04},
  193. {0x91, 0xb0},
  194. {0x45, 0x01},
  195. {0x46, 0x00},
  196. {0x47, 0x6c},
  197. {0x48, 0x03},
  198. {0x49, 0x8b},
  199. {0x4a, 0x00},
  200. {0x4b, 0x07},
  201. {0x4c, 0x04},
  202. {0x4d, 0xb7},
  203. {0xf0, 0x40},
  204. {0xf1, 0x40},
  205. {0xf2, 0x40},
  206. {0xf3, 0x40},
  207. {0x3f, 0x00},
  208. {0xfd, 0x01},
  209. {0x05, 0x00},
  210. {0x06, 0xa6},
  211. {0xfd, 0x01},
  212. };
  213. static const char * const ov02a10_test_pattern_menu[] = {
  214. "Disabled",
  215. "Eight Vertical Colour Bars",
  216. };
  217. static const s64 link_freq_menu_items[] = {
  218. OV02A10_LINK_FREQ_390MHZ,
  219. };
  220. static u64 to_pixel_rate(u32 f_index)
  221. {
  222. u64 pixel_rate = link_freq_menu_items[f_index] * 2 * OV02A10_DATA_LANES;
  223. do_div(pixel_rate, OV02A10_BITS_PER_SAMPLE);
  224. return pixel_rate;
  225. }
  226. static const struct ov02a10_mode supported_modes[] = {
  227. {
  228. .width = 1600,
  229. .height = 1200,
  230. .exp_def = 0x01ae,
  231. .hts_def = 0x03a6,
  232. .vts_def = 0x056e,
  233. .reg_list = {
  234. .num_of_regs = ARRAY_SIZE(ov02a10_1600x1200_regs),
  235. .regs = ov02a10_1600x1200_regs,
  236. },
  237. },
  238. };
  239. static int ov02a10_write_array(struct ov02a10 *ov02a10,
  240. const struct ov02a10_reg_list *r_list)
  241. {
  242. struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev);
  243. unsigned int i;
  244. int ret;
  245. for (i = 0; i < r_list->num_of_regs; i++) {
  246. ret = i2c_smbus_write_byte_data(client, r_list->regs[i].addr,
  247. r_list->regs[i].val);
  248. if (ret < 0)
  249. return ret;
  250. }
  251. return 0;
  252. }
  253. static void ov02a10_fill_fmt(const struct ov02a10_mode *mode,
  254. struct v4l2_mbus_framefmt *fmt)
  255. {
  256. fmt->width = mode->width;
  257. fmt->height = mode->height;
  258. fmt->field = V4L2_FIELD_NONE;
  259. }
  260. static int ov02a10_set_fmt(struct v4l2_subdev *sd,
  261. struct v4l2_subdev_state *sd_state,
  262. struct v4l2_subdev_format *fmt)
  263. {
  264. struct ov02a10 *ov02a10 = to_ov02a10(sd);
  265. struct v4l2_mbus_framefmt *mbus_fmt = &fmt->format;
  266. struct v4l2_mbus_framefmt *frame_fmt;
  267. int ret = 0;
  268. mutex_lock(&ov02a10->mutex);
  269. if (ov02a10->streaming && fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  270. ret = -EBUSY;
  271. goto out_unlock;
  272. }
  273. /* Only one sensor mode supported */
  274. mbus_fmt->code = ov02a10->fmt.code;
  275. ov02a10_fill_fmt(ov02a10->cur_mode, mbus_fmt);
  276. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
  277. frame_fmt = v4l2_subdev_state_get_format(sd_state, 0);
  278. else
  279. frame_fmt = &ov02a10->fmt;
  280. *frame_fmt = *mbus_fmt;
  281. out_unlock:
  282. mutex_unlock(&ov02a10->mutex);
  283. return ret;
  284. }
  285. static int ov02a10_get_fmt(struct v4l2_subdev *sd,
  286. struct v4l2_subdev_state *sd_state,
  287. struct v4l2_subdev_format *fmt)
  288. {
  289. struct ov02a10 *ov02a10 = to_ov02a10(sd);
  290. struct v4l2_mbus_framefmt *mbus_fmt = &fmt->format;
  291. mutex_lock(&ov02a10->mutex);
  292. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  293. fmt->format = *v4l2_subdev_state_get_format(sd_state,
  294. fmt->pad);
  295. } else {
  296. fmt->format = ov02a10->fmt;
  297. mbus_fmt->code = ov02a10->fmt.code;
  298. ov02a10_fill_fmt(ov02a10->cur_mode, mbus_fmt);
  299. }
  300. mutex_unlock(&ov02a10->mutex);
  301. return 0;
  302. }
  303. static int ov02a10_enum_mbus_code(struct v4l2_subdev *sd,
  304. struct v4l2_subdev_state *sd_state,
  305. struct v4l2_subdev_mbus_code_enum *code)
  306. {
  307. struct ov02a10 *ov02a10 = to_ov02a10(sd);
  308. if (code->index != 0)
  309. return -EINVAL;
  310. code->code = ov02a10->fmt.code;
  311. return 0;
  312. }
  313. static int ov02a10_enum_frame_sizes(struct v4l2_subdev *sd,
  314. struct v4l2_subdev_state *sd_state,
  315. struct v4l2_subdev_frame_size_enum *fse)
  316. {
  317. if (fse->index >= ARRAY_SIZE(supported_modes))
  318. return -EINVAL;
  319. fse->min_width = supported_modes[fse->index].width;
  320. fse->max_width = supported_modes[fse->index].width;
  321. fse->max_height = supported_modes[fse->index].height;
  322. fse->min_height = supported_modes[fse->index].height;
  323. return 0;
  324. }
  325. static int ov02a10_check_sensor_id(struct ov02a10 *ov02a10)
  326. {
  327. struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev);
  328. u16 chip_id;
  329. int ret;
  330. /* Validate the chip ID */
  331. ret = i2c_smbus_read_word_swapped(client, OV02A10_REG_CHIP_ID);
  332. if (ret < 0)
  333. return ret;
  334. chip_id = le16_to_cpu((__force __le16)ret);
  335. if ((chip_id & OV02A10_ID_MASK) != OV02A10_ID) {
  336. dev_err(ov02a10->dev, "unexpected sensor id(0x%04x)\n", chip_id);
  337. return -EINVAL;
  338. }
  339. return 0;
  340. }
  341. static int ov02a10_power_on(struct device *dev)
  342. {
  343. struct i2c_client *client = to_i2c_client(dev);
  344. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  345. struct ov02a10 *ov02a10 = to_ov02a10(sd);
  346. int ret;
  347. gpiod_set_value_cansleep(ov02a10->rst_gpio, 1);
  348. gpiod_set_value_cansleep(ov02a10->pd_gpio, 1);
  349. ret = clk_prepare_enable(ov02a10->eclk);
  350. if (ret < 0) {
  351. dev_err(dev, "failed to enable eclk\n");
  352. return ret;
  353. }
  354. ret = regulator_bulk_enable(ARRAY_SIZE(ov02a10_supply_names),
  355. ov02a10->supplies);
  356. if (ret < 0) {
  357. dev_err(dev, "failed to enable regulators\n");
  358. goto disable_clk;
  359. }
  360. usleep_range(5000, 6000);
  361. gpiod_set_value_cansleep(ov02a10->pd_gpio, 0);
  362. usleep_range(5000, 6000);
  363. gpiod_set_value_cansleep(ov02a10->rst_gpio, 0);
  364. usleep_range(5000, 6000);
  365. ret = ov02a10_check_sensor_id(ov02a10);
  366. if (ret)
  367. goto disable_regulator;
  368. return 0;
  369. disable_regulator:
  370. regulator_bulk_disable(ARRAY_SIZE(ov02a10_supply_names),
  371. ov02a10->supplies);
  372. disable_clk:
  373. clk_disable_unprepare(ov02a10->eclk);
  374. return ret;
  375. }
  376. static int ov02a10_power_off(struct device *dev)
  377. {
  378. struct i2c_client *client = to_i2c_client(dev);
  379. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  380. struct ov02a10 *ov02a10 = to_ov02a10(sd);
  381. gpiod_set_value_cansleep(ov02a10->rst_gpio, 1);
  382. clk_disable_unprepare(ov02a10->eclk);
  383. gpiod_set_value_cansleep(ov02a10->pd_gpio, 1);
  384. regulator_bulk_disable(ARRAY_SIZE(ov02a10_supply_names),
  385. ov02a10->supplies);
  386. return 0;
  387. }
  388. static int __ov02a10_start_stream(struct ov02a10 *ov02a10)
  389. {
  390. struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev);
  391. const struct ov02a10_reg_list *reg_list;
  392. int ret;
  393. /* Apply default values of current mode */
  394. reg_list = &ov02a10->cur_mode->reg_list;
  395. ret = ov02a10_write_array(ov02a10, reg_list);
  396. if (ret)
  397. return ret;
  398. /* Apply customized values from user */
  399. ret = __v4l2_ctrl_handler_setup(ov02a10->subdev.ctrl_handler);
  400. if (ret)
  401. return ret;
  402. /* Set orientation to 180 degree */
  403. if (ov02a10->upside_down) {
  404. ret = i2c_smbus_write_byte_data(client, REG_MIRROR_FLIP_CONTROL,
  405. REG_MIRROR_FLIP_ENABLE);
  406. if (ret < 0) {
  407. dev_err(ov02a10->dev, "failed to set orientation\n");
  408. return ret;
  409. }
  410. ret = i2c_smbus_write_byte_data(client, REG_GLOBAL_EFFECTIVE,
  411. REG_ENABLE);
  412. if (ret < 0)
  413. return ret;
  414. }
  415. /* Set MIPI TX speed according to DT property */
  416. if (ov02a10->mipi_clock_voltage != OV02A10_MIPI_TX_SPEED_DEFAULT) {
  417. ret = i2c_smbus_write_byte_data(client, TX_SPEED_AREA_SEL,
  418. ov02a10->mipi_clock_voltage);
  419. if (ret < 0)
  420. return ret;
  421. }
  422. /* Set stream on register */
  423. return i2c_smbus_write_byte_data(client, REG_SC_CTRL_MODE,
  424. SC_CTRL_MODE_STREAMING);
  425. }
  426. static int __ov02a10_stop_stream(struct ov02a10 *ov02a10)
  427. {
  428. struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev);
  429. return i2c_smbus_write_byte_data(client, REG_SC_CTRL_MODE,
  430. SC_CTRL_MODE_STANDBY);
  431. }
  432. static int ov02a10_init_state(struct v4l2_subdev *sd,
  433. struct v4l2_subdev_state *sd_state)
  434. {
  435. struct v4l2_subdev_format fmt = {
  436. .which = V4L2_SUBDEV_FORMAT_TRY,
  437. .format = {
  438. .width = 1600,
  439. .height = 1200,
  440. }
  441. };
  442. ov02a10_set_fmt(sd, sd_state, &fmt);
  443. return 0;
  444. }
  445. static int ov02a10_s_stream(struct v4l2_subdev *sd, int on)
  446. {
  447. struct ov02a10 *ov02a10 = to_ov02a10(sd);
  448. int ret;
  449. mutex_lock(&ov02a10->mutex);
  450. if (ov02a10->streaming == on) {
  451. ret = 0;
  452. goto unlock_and_return;
  453. }
  454. if (on) {
  455. ret = pm_runtime_resume_and_get(ov02a10->dev);
  456. if (ret < 0)
  457. goto unlock_and_return;
  458. ret = __ov02a10_start_stream(ov02a10);
  459. if (ret) {
  460. __ov02a10_stop_stream(ov02a10);
  461. ov02a10->streaming = !on;
  462. goto err_rpm_put;
  463. }
  464. } else {
  465. __ov02a10_stop_stream(ov02a10);
  466. pm_runtime_put(ov02a10->dev);
  467. }
  468. ov02a10->streaming = on;
  469. mutex_unlock(&ov02a10->mutex);
  470. return 0;
  471. err_rpm_put:
  472. pm_runtime_put(ov02a10->dev);
  473. unlock_and_return:
  474. mutex_unlock(&ov02a10->mutex);
  475. return ret;
  476. }
  477. static const struct dev_pm_ops ov02a10_pm_ops = {
  478. SET_RUNTIME_PM_OPS(ov02a10_power_off, ov02a10_power_on, NULL)
  479. };
  480. static int ov02a10_set_exposure(struct ov02a10 *ov02a10, int val)
  481. {
  482. struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev);
  483. int ret;
  484. ret = i2c_smbus_write_byte_data(client, REG_PAGE_SWITCH, REG_ENABLE);
  485. if (ret < 0)
  486. return ret;
  487. ret = i2c_smbus_write_byte_data(client, OV02A10_REG_EXPOSURE_H,
  488. val >> OV02A10_EXP_SHIFT);
  489. if (ret < 0)
  490. return ret;
  491. ret = i2c_smbus_write_byte_data(client, OV02A10_REG_EXPOSURE_L, val);
  492. if (ret < 0)
  493. return ret;
  494. return i2c_smbus_write_byte_data(client, REG_GLOBAL_EFFECTIVE,
  495. REG_ENABLE);
  496. }
  497. static int ov02a10_set_gain(struct ov02a10 *ov02a10, int val)
  498. {
  499. struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev);
  500. int ret;
  501. ret = i2c_smbus_write_byte_data(client, REG_PAGE_SWITCH, REG_ENABLE);
  502. if (ret < 0)
  503. return ret;
  504. ret = i2c_smbus_write_byte_data(client, OV02A10_REG_GAIN, val);
  505. if (ret < 0)
  506. return ret;
  507. return i2c_smbus_write_byte_data(client, REG_GLOBAL_EFFECTIVE,
  508. REG_ENABLE);
  509. }
  510. static int ov02a10_set_vblank(struct ov02a10 *ov02a10, int val)
  511. {
  512. struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev);
  513. u32 vts = val + ov02a10->cur_mode->height - OV02A10_BASE_LINES;
  514. int ret;
  515. ret = i2c_smbus_write_byte_data(client, REG_PAGE_SWITCH, REG_ENABLE);
  516. if (ret < 0)
  517. return ret;
  518. ret = i2c_smbus_write_byte_data(client, OV02A10_REG_VTS_H,
  519. vts >> OV02A10_VTS_SHIFT);
  520. if (ret < 0)
  521. return ret;
  522. ret = i2c_smbus_write_byte_data(client, OV02A10_REG_VTS_L, vts);
  523. if (ret < 0)
  524. return ret;
  525. return i2c_smbus_write_byte_data(client, REG_GLOBAL_EFFECTIVE,
  526. REG_ENABLE);
  527. }
  528. static int ov02a10_set_test_pattern(struct ov02a10 *ov02a10, int pattern)
  529. {
  530. struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev);
  531. int ret;
  532. ret = i2c_smbus_write_byte_data(client, REG_PAGE_SWITCH, REG_ENABLE);
  533. if (ret < 0)
  534. return ret;
  535. ret = i2c_smbus_write_byte_data(client, OV02A10_REG_TEST_PATTERN,
  536. pattern);
  537. if (ret < 0)
  538. return ret;
  539. ret = i2c_smbus_write_byte_data(client, REG_GLOBAL_EFFECTIVE,
  540. REG_ENABLE);
  541. if (ret < 0)
  542. return ret;
  543. return i2c_smbus_write_byte_data(client, REG_SC_CTRL_MODE,
  544. SC_CTRL_MODE_STREAMING);
  545. }
  546. static int ov02a10_set_ctrl(struct v4l2_ctrl *ctrl)
  547. {
  548. struct ov02a10 *ov02a10 = container_of(ctrl->handler,
  549. struct ov02a10, ctrl_handler);
  550. s64 max_expo;
  551. int ret;
  552. /* Propagate change of current control to all related controls */
  553. if (ctrl->id == V4L2_CID_VBLANK) {
  554. /* Update max exposure while meeting expected vblanking */
  555. max_expo = ov02a10->cur_mode->height + ctrl->val -
  556. OV02A10_EXPOSURE_MAX_MARGIN;
  557. __v4l2_ctrl_modify_range(ov02a10->exposure,
  558. ov02a10->exposure->minimum, max_expo,
  559. ov02a10->exposure->step,
  560. ov02a10->exposure->default_value);
  561. }
  562. /* V4L2 controls values will be applied only when power is already up */
  563. if (!pm_runtime_get_if_in_use(ov02a10->dev))
  564. return 0;
  565. switch (ctrl->id) {
  566. case V4L2_CID_EXPOSURE:
  567. ret = ov02a10_set_exposure(ov02a10, ctrl->val);
  568. break;
  569. case V4L2_CID_ANALOGUE_GAIN:
  570. ret = ov02a10_set_gain(ov02a10, ctrl->val);
  571. break;
  572. case V4L2_CID_VBLANK:
  573. ret = ov02a10_set_vblank(ov02a10, ctrl->val);
  574. break;
  575. case V4L2_CID_TEST_PATTERN:
  576. ret = ov02a10_set_test_pattern(ov02a10, ctrl->val);
  577. break;
  578. default:
  579. ret = -EINVAL;
  580. break;
  581. }
  582. pm_runtime_put(ov02a10->dev);
  583. return ret;
  584. }
  585. static const struct v4l2_subdev_video_ops ov02a10_video_ops = {
  586. .s_stream = ov02a10_s_stream,
  587. };
  588. static const struct v4l2_subdev_pad_ops ov02a10_pad_ops = {
  589. .enum_mbus_code = ov02a10_enum_mbus_code,
  590. .enum_frame_size = ov02a10_enum_frame_sizes,
  591. .get_fmt = ov02a10_get_fmt,
  592. .set_fmt = ov02a10_set_fmt,
  593. };
  594. static const struct v4l2_subdev_ops ov02a10_subdev_ops = {
  595. .video = &ov02a10_video_ops,
  596. .pad = &ov02a10_pad_ops,
  597. };
  598. static const struct v4l2_subdev_internal_ops ov02a10_internal_ops = {
  599. .init_state = ov02a10_init_state,
  600. };
  601. static const struct media_entity_operations ov02a10_subdev_entity_ops = {
  602. .link_validate = v4l2_subdev_link_validate,
  603. };
  604. static const struct v4l2_ctrl_ops ov02a10_ctrl_ops = {
  605. .s_ctrl = ov02a10_set_ctrl,
  606. };
  607. static int ov02a10_initialize_controls(struct ov02a10 *ov02a10)
  608. {
  609. const struct ov02a10_mode *mode;
  610. struct v4l2_ctrl_handler *handler;
  611. struct v4l2_ctrl *ctrl;
  612. s64 exposure_max;
  613. s64 vblank_def;
  614. s64 pixel_rate;
  615. s64 h_blank;
  616. int ret;
  617. handler = &ov02a10->ctrl_handler;
  618. mode = ov02a10->cur_mode;
  619. ret = v4l2_ctrl_handler_init(handler, 7);
  620. if (ret)
  621. return ret;
  622. handler->lock = &ov02a10->mutex;
  623. ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ, 0, 0,
  624. link_freq_menu_items);
  625. if (ctrl)
  626. ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  627. pixel_rate = to_pixel_rate(0);
  628. v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE, 0, pixel_rate, 1,
  629. pixel_rate);
  630. h_blank = mode->hts_def - mode->width;
  631. v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK, h_blank, h_blank, 1,
  632. h_blank);
  633. vblank_def = mode->vts_def - mode->height;
  634. v4l2_ctrl_new_std(handler, &ov02a10_ctrl_ops, V4L2_CID_VBLANK,
  635. vblank_def, OV02A10_VTS_MAX - mode->height, 1,
  636. vblank_def);
  637. exposure_max = mode->vts_def - 4;
  638. ov02a10->exposure = v4l2_ctrl_new_std(handler, &ov02a10_ctrl_ops,
  639. V4L2_CID_EXPOSURE,
  640. OV02A10_EXPOSURE_MIN,
  641. exposure_max,
  642. OV02A10_EXPOSURE_STEP,
  643. mode->exp_def);
  644. v4l2_ctrl_new_std(handler, &ov02a10_ctrl_ops,
  645. V4L2_CID_ANALOGUE_GAIN, OV02A10_GAIN_MIN,
  646. OV02A10_GAIN_MAX, OV02A10_GAIN_STEP,
  647. OV02A10_GAIN_DEFAULT);
  648. v4l2_ctrl_new_std_menu_items(handler, &ov02a10_ctrl_ops,
  649. V4L2_CID_TEST_PATTERN,
  650. ARRAY_SIZE(ov02a10_test_pattern_menu) - 1,
  651. 0, 0, ov02a10_test_pattern_menu);
  652. if (handler->error) {
  653. ret = handler->error;
  654. dev_err(ov02a10->dev, "failed to init controls(%d)\n", ret);
  655. goto err_free_handler;
  656. }
  657. ov02a10->subdev.ctrl_handler = handler;
  658. return 0;
  659. err_free_handler:
  660. v4l2_ctrl_handler_free(handler);
  661. return ret;
  662. }
  663. static int ov02a10_check_hwcfg(struct device *dev, struct ov02a10 *ov02a10)
  664. {
  665. struct fwnode_handle *ep;
  666. struct fwnode_handle *fwnode = dev_fwnode(dev);
  667. struct v4l2_fwnode_endpoint bus_cfg = {
  668. .bus_type = V4L2_MBUS_CSI2_DPHY,
  669. };
  670. unsigned int i, j;
  671. u32 clk_volt;
  672. int ret;
  673. if (!fwnode)
  674. return -EINVAL;
  675. ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
  676. if (!ep)
  677. return -ENXIO;
  678. ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
  679. fwnode_handle_put(ep);
  680. if (ret)
  681. return ret;
  682. /* Optional indication of MIPI clock voltage unit */
  683. ret = fwnode_property_read_u32(ep, "ovti,mipi-clock-voltage",
  684. &clk_volt);
  685. if (!ret)
  686. ov02a10->mipi_clock_voltage = clk_volt;
  687. for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
  688. for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
  689. if (link_freq_menu_items[i] ==
  690. bus_cfg.link_frequencies[j])
  691. break;
  692. }
  693. if (j == bus_cfg.nr_of_link_frequencies) {
  694. dev_err(dev, "no link frequency %lld supported\n",
  695. link_freq_menu_items[i]);
  696. ret = -EINVAL;
  697. break;
  698. }
  699. }
  700. v4l2_fwnode_endpoint_free(&bus_cfg);
  701. return ret;
  702. }
  703. static int ov02a10_probe(struct i2c_client *client)
  704. {
  705. struct device *dev = &client->dev;
  706. struct ov02a10 *ov02a10;
  707. unsigned int i;
  708. unsigned int rotation;
  709. int ret;
  710. ov02a10 = devm_kzalloc(dev, sizeof(*ov02a10), GFP_KERNEL);
  711. if (!ov02a10)
  712. return -ENOMEM;
  713. ov02a10->dev = dev;
  714. ret = ov02a10_check_hwcfg(dev, ov02a10);
  715. if (ret)
  716. return dev_err_probe(dev, ret,
  717. "failed to check HW configuration\n");
  718. v4l2_i2c_subdev_init(&ov02a10->subdev, client, &ov02a10_subdev_ops);
  719. ov02a10->subdev.internal_ops = &ov02a10_internal_ops;
  720. ov02a10->mipi_clock_voltage = OV02A10_MIPI_TX_SPEED_DEFAULT;
  721. ov02a10->fmt.code = MEDIA_BUS_FMT_SBGGR10_1X10;
  722. /* Optional indication of physical rotation of sensor */
  723. rotation = 0;
  724. device_property_read_u32(dev, "rotation", &rotation);
  725. if (rotation == 180) {
  726. ov02a10->upside_down = true;
  727. ov02a10->fmt.code = MEDIA_BUS_FMT_SRGGB10_1X10;
  728. }
  729. ov02a10->eclk = devm_v4l2_sensor_clk_get_legacy(dev, "eclk", false, 0);
  730. if (IS_ERR(ov02a10->eclk))
  731. return dev_err_probe(dev, PTR_ERR(ov02a10->eclk),
  732. "failed to get eclk\n");
  733. if (clk_get_rate(ov02a10->eclk) != OV02A10_ECLK_FREQ)
  734. dev_warn(dev, "eclk mismatched, mode is based on 24MHz\n");
  735. ov02a10->pd_gpio = devm_gpiod_get(dev, "powerdown", GPIOD_OUT_HIGH);
  736. if (IS_ERR(ov02a10->pd_gpio))
  737. return dev_err_probe(dev, PTR_ERR(ov02a10->pd_gpio),
  738. "failed to get powerdown-gpios\n");
  739. ov02a10->rst_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  740. if (IS_ERR(ov02a10->rst_gpio))
  741. return dev_err_probe(dev, PTR_ERR(ov02a10->rst_gpio),
  742. "failed to get reset-gpios\n");
  743. for (i = 0; i < ARRAY_SIZE(ov02a10_supply_names); i++)
  744. ov02a10->supplies[i].supply = ov02a10_supply_names[i];
  745. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ov02a10_supply_names),
  746. ov02a10->supplies);
  747. if (ret)
  748. return dev_err_probe(dev, ret, "failed to get regulators\n");
  749. mutex_init(&ov02a10->mutex);
  750. /* Set default mode */
  751. ov02a10->cur_mode = &supported_modes[0];
  752. ret = ov02a10_initialize_controls(ov02a10);
  753. if (ret) {
  754. dev_err_probe(dev, ret, "failed to initialize controls\n");
  755. goto err_destroy_mutex;
  756. }
  757. /* Initialize subdev */
  758. ov02a10->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  759. ov02a10->subdev.entity.ops = &ov02a10_subdev_entity_ops;
  760. ov02a10->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  761. ov02a10->pad.flags = MEDIA_PAD_FL_SOURCE;
  762. ret = media_entity_pads_init(&ov02a10->subdev.entity, 1, &ov02a10->pad);
  763. if (ret < 0) {
  764. dev_err_probe(dev, ret, "failed to initialize entity pads\n");
  765. goto err_free_handler;
  766. }
  767. pm_runtime_enable(dev);
  768. if (!pm_runtime_enabled(dev)) {
  769. ret = ov02a10_power_on(dev);
  770. if (ret < 0) {
  771. dev_err_probe(dev, ret, "failed to power on\n");
  772. goto err_clean_entity;
  773. }
  774. }
  775. ret = v4l2_async_register_subdev(&ov02a10->subdev);
  776. if (ret) {
  777. dev_err_probe(dev, ret, "failed to register V4L2 subdev\n");
  778. goto err_power_off;
  779. }
  780. return 0;
  781. err_power_off:
  782. if (pm_runtime_enabled(dev))
  783. pm_runtime_disable(dev);
  784. else
  785. ov02a10_power_off(dev);
  786. err_clean_entity:
  787. media_entity_cleanup(&ov02a10->subdev.entity);
  788. err_free_handler:
  789. v4l2_ctrl_handler_free(ov02a10->subdev.ctrl_handler);
  790. err_destroy_mutex:
  791. mutex_destroy(&ov02a10->mutex);
  792. return ret;
  793. }
  794. static void ov02a10_remove(struct i2c_client *client)
  795. {
  796. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  797. struct ov02a10 *ov02a10 = to_ov02a10(sd);
  798. v4l2_async_unregister_subdev(sd);
  799. media_entity_cleanup(&sd->entity);
  800. v4l2_ctrl_handler_free(sd->ctrl_handler);
  801. pm_runtime_disable(ov02a10->dev);
  802. if (!pm_runtime_status_suspended(ov02a10->dev))
  803. ov02a10_power_off(ov02a10->dev);
  804. pm_runtime_set_suspended(ov02a10->dev);
  805. mutex_destroy(&ov02a10->mutex);
  806. }
  807. static const struct of_device_id ov02a10_of_match[] = {
  808. { .compatible = "ovti,ov02a10" },
  809. {}
  810. };
  811. MODULE_DEVICE_TABLE(of, ov02a10_of_match);
  812. static struct i2c_driver ov02a10_i2c_driver = {
  813. .driver = {
  814. .name = "ov02a10",
  815. .pm = &ov02a10_pm_ops,
  816. .of_match_table = ov02a10_of_match,
  817. },
  818. .probe = ov02a10_probe,
  819. .remove = ov02a10_remove,
  820. };
  821. module_i2c_driver(ov02a10_i2c_driver);
  822. MODULE_AUTHOR("Dongchun Zhu <dongchun.zhu@mediatek.com>");
  823. MODULE_DESCRIPTION("OmniVision OV02A10 sensor driver");
  824. MODULE_LICENSE("GPL v2");