ov02a10.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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. u32 eclk_freq;
  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(&client->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(&client->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. struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev);
  449. int ret;
  450. mutex_lock(&ov02a10->mutex);
  451. if (ov02a10->streaming == on) {
  452. ret = 0;
  453. goto unlock_and_return;
  454. }
  455. if (on) {
  456. ret = pm_runtime_resume_and_get(&client->dev);
  457. if (ret < 0)
  458. goto unlock_and_return;
  459. ret = __ov02a10_start_stream(ov02a10);
  460. if (ret) {
  461. __ov02a10_stop_stream(ov02a10);
  462. ov02a10->streaming = !on;
  463. goto err_rpm_put;
  464. }
  465. } else {
  466. __ov02a10_stop_stream(ov02a10);
  467. pm_runtime_put(&client->dev);
  468. }
  469. ov02a10->streaming = on;
  470. mutex_unlock(&ov02a10->mutex);
  471. return 0;
  472. err_rpm_put:
  473. pm_runtime_put(&client->dev);
  474. unlock_and_return:
  475. mutex_unlock(&ov02a10->mutex);
  476. return ret;
  477. }
  478. static const struct dev_pm_ops ov02a10_pm_ops = {
  479. SET_RUNTIME_PM_OPS(ov02a10_power_off, ov02a10_power_on, NULL)
  480. };
  481. static int ov02a10_set_exposure(struct ov02a10 *ov02a10, int val)
  482. {
  483. struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev);
  484. int ret;
  485. ret = i2c_smbus_write_byte_data(client, REG_PAGE_SWITCH, REG_ENABLE);
  486. if (ret < 0)
  487. return ret;
  488. ret = i2c_smbus_write_byte_data(client, OV02A10_REG_EXPOSURE_H,
  489. val >> OV02A10_EXP_SHIFT);
  490. if (ret < 0)
  491. return ret;
  492. ret = i2c_smbus_write_byte_data(client, OV02A10_REG_EXPOSURE_L, val);
  493. if (ret < 0)
  494. return ret;
  495. return i2c_smbus_write_byte_data(client, REG_GLOBAL_EFFECTIVE,
  496. REG_ENABLE);
  497. }
  498. static int ov02a10_set_gain(struct ov02a10 *ov02a10, int val)
  499. {
  500. struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev);
  501. int ret;
  502. ret = i2c_smbus_write_byte_data(client, REG_PAGE_SWITCH, REG_ENABLE);
  503. if (ret < 0)
  504. return ret;
  505. ret = i2c_smbus_write_byte_data(client, OV02A10_REG_GAIN, val);
  506. if (ret < 0)
  507. return ret;
  508. return i2c_smbus_write_byte_data(client, REG_GLOBAL_EFFECTIVE,
  509. REG_ENABLE);
  510. }
  511. static int ov02a10_set_vblank(struct ov02a10 *ov02a10, int val)
  512. {
  513. struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev);
  514. u32 vts = val + ov02a10->cur_mode->height - OV02A10_BASE_LINES;
  515. int ret;
  516. ret = i2c_smbus_write_byte_data(client, REG_PAGE_SWITCH, REG_ENABLE);
  517. if (ret < 0)
  518. return ret;
  519. ret = i2c_smbus_write_byte_data(client, OV02A10_REG_VTS_H,
  520. vts >> OV02A10_VTS_SHIFT);
  521. if (ret < 0)
  522. return ret;
  523. ret = i2c_smbus_write_byte_data(client, OV02A10_REG_VTS_L, vts);
  524. if (ret < 0)
  525. return ret;
  526. return i2c_smbus_write_byte_data(client, REG_GLOBAL_EFFECTIVE,
  527. REG_ENABLE);
  528. }
  529. static int ov02a10_set_test_pattern(struct ov02a10 *ov02a10, int pattern)
  530. {
  531. struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev);
  532. int ret;
  533. ret = i2c_smbus_write_byte_data(client, REG_PAGE_SWITCH, REG_ENABLE);
  534. if (ret < 0)
  535. return ret;
  536. ret = i2c_smbus_write_byte_data(client, OV02A10_REG_TEST_PATTERN,
  537. pattern);
  538. if (ret < 0)
  539. return ret;
  540. ret = i2c_smbus_write_byte_data(client, REG_GLOBAL_EFFECTIVE,
  541. REG_ENABLE);
  542. if (ret < 0)
  543. return ret;
  544. return i2c_smbus_write_byte_data(client, REG_SC_CTRL_MODE,
  545. SC_CTRL_MODE_STREAMING);
  546. }
  547. static int ov02a10_set_ctrl(struct v4l2_ctrl *ctrl)
  548. {
  549. struct ov02a10 *ov02a10 = container_of(ctrl->handler,
  550. struct ov02a10, ctrl_handler);
  551. struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev);
  552. s64 max_expo;
  553. int ret;
  554. /* Propagate change of current control to all related controls */
  555. if (ctrl->id == V4L2_CID_VBLANK) {
  556. /* Update max exposure while meeting expected vblanking */
  557. max_expo = ov02a10->cur_mode->height + ctrl->val -
  558. OV02A10_EXPOSURE_MAX_MARGIN;
  559. __v4l2_ctrl_modify_range(ov02a10->exposure,
  560. ov02a10->exposure->minimum, max_expo,
  561. ov02a10->exposure->step,
  562. ov02a10->exposure->default_value);
  563. }
  564. /* V4L2 controls values will be applied only when power is already up */
  565. if (!pm_runtime_get_if_in_use(&client->dev))
  566. return 0;
  567. switch (ctrl->id) {
  568. case V4L2_CID_EXPOSURE:
  569. ret = ov02a10_set_exposure(ov02a10, ctrl->val);
  570. break;
  571. case V4L2_CID_ANALOGUE_GAIN:
  572. ret = ov02a10_set_gain(ov02a10, ctrl->val);
  573. break;
  574. case V4L2_CID_VBLANK:
  575. ret = ov02a10_set_vblank(ov02a10, ctrl->val);
  576. break;
  577. case V4L2_CID_TEST_PATTERN:
  578. ret = ov02a10_set_test_pattern(ov02a10, ctrl->val);
  579. break;
  580. default:
  581. ret = -EINVAL;
  582. break;
  583. }
  584. pm_runtime_put(&client->dev);
  585. return ret;
  586. }
  587. static const struct v4l2_subdev_video_ops ov02a10_video_ops = {
  588. .s_stream = ov02a10_s_stream,
  589. };
  590. static const struct v4l2_subdev_pad_ops ov02a10_pad_ops = {
  591. .enum_mbus_code = ov02a10_enum_mbus_code,
  592. .enum_frame_size = ov02a10_enum_frame_sizes,
  593. .get_fmt = ov02a10_get_fmt,
  594. .set_fmt = ov02a10_set_fmt,
  595. };
  596. static const struct v4l2_subdev_ops ov02a10_subdev_ops = {
  597. .video = &ov02a10_video_ops,
  598. .pad = &ov02a10_pad_ops,
  599. };
  600. static const struct v4l2_subdev_internal_ops ov02a10_internal_ops = {
  601. .init_state = ov02a10_init_state,
  602. };
  603. static const struct media_entity_operations ov02a10_subdev_entity_ops = {
  604. .link_validate = v4l2_subdev_link_validate,
  605. };
  606. static const struct v4l2_ctrl_ops ov02a10_ctrl_ops = {
  607. .s_ctrl = ov02a10_set_ctrl,
  608. };
  609. static int ov02a10_initialize_controls(struct ov02a10 *ov02a10)
  610. {
  611. struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev);
  612. const struct ov02a10_mode *mode;
  613. struct v4l2_ctrl_handler *handler;
  614. struct v4l2_ctrl *ctrl;
  615. s64 exposure_max;
  616. s64 vblank_def;
  617. s64 pixel_rate;
  618. s64 h_blank;
  619. int ret;
  620. handler = &ov02a10->ctrl_handler;
  621. mode = ov02a10->cur_mode;
  622. ret = v4l2_ctrl_handler_init(handler, 7);
  623. if (ret)
  624. return ret;
  625. handler->lock = &ov02a10->mutex;
  626. ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ, 0, 0,
  627. link_freq_menu_items);
  628. if (ctrl)
  629. ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  630. pixel_rate = to_pixel_rate(0);
  631. v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE, 0, pixel_rate, 1,
  632. pixel_rate);
  633. h_blank = mode->hts_def - mode->width;
  634. v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK, h_blank, h_blank, 1,
  635. h_blank);
  636. vblank_def = mode->vts_def - mode->height;
  637. v4l2_ctrl_new_std(handler, &ov02a10_ctrl_ops, V4L2_CID_VBLANK,
  638. vblank_def, OV02A10_VTS_MAX - mode->height, 1,
  639. vblank_def);
  640. exposure_max = mode->vts_def - 4;
  641. ov02a10->exposure = v4l2_ctrl_new_std(handler, &ov02a10_ctrl_ops,
  642. V4L2_CID_EXPOSURE,
  643. OV02A10_EXPOSURE_MIN,
  644. exposure_max,
  645. OV02A10_EXPOSURE_STEP,
  646. mode->exp_def);
  647. v4l2_ctrl_new_std(handler, &ov02a10_ctrl_ops,
  648. V4L2_CID_ANALOGUE_GAIN, OV02A10_GAIN_MIN,
  649. OV02A10_GAIN_MAX, OV02A10_GAIN_STEP,
  650. OV02A10_GAIN_DEFAULT);
  651. v4l2_ctrl_new_std_menu_items(handler, &ov02a10_ctrl_ops,
  652. V4L2_CID_TEST_PATTERN,
  653. ARRAY_SIZE(ov02a10_test_pattern_menu) - 1,
  654. 0, 0, ov02a10_test_pattern_menu);
  655. if (handler->error) {
  656. ret = handler->error;
  657. dev_err(&client->dev, "failed to init controls(%d)\n", ret);
  658. goto err_free_handler;
  659. }
  660. ov02a10->subdev.ctrl_handler = handler;
  661. return 0;
  662. err_free_handler:
  663. v4l2_ctrl_handler_free(handler);
  664. return ret;
  665. }
  666. static int ov02a10_check_hwcfg(struct device *dev, struct ov02a10 *ov02a10)
  667. {
  668. struct fwnode_handle *ep;
  669. struct fwnode_handle *fwnode = dev_fwnode(dev);
  670. struct v4l2_fwnode_endpoint bus_cfg = {
  671. .bus_type = V4L2_MBUS_CSI2_DPHY,
  672. };
  673. unsigned int i, j;
  674. u32 clk_volt;
  675. int ret;
  676. if (!fwnode)
  677. return -EINVAL;
  678. ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
  679. if (!ep)
  680. return -ENXIO;
  681. ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
  682. fwnode_handle_put(ep);
  683. if (ret)
  684. return ret;
  685. /* Optional indication of MIPI clock voltage unit */
  686. ret = fwnode_property_read_u32(ep, "ovti,mipi-clock-voltage",
  687. &clk_volt);
  688. if (!ret)
  689. ov02a10->mipi_clock_voltage = clk_volt;
  690. for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
  691. for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
  692. if (link_freq_menu_items[i] ==
  693. bus_cfg.link_frequencies[j])
  694. break;
  695. }
  696. if (j == bus_cfg.nr_of_link_frequencies) {
  697. dev_err(dev, "no link frequency %lld supported\n",
  698. link_freq_menu_items[i]);
  699. ret = -EINVAL;
  700. break;
  701. }
  702. }
  703. v4l2_fwnode_endpoint_free(&bus_cfg);
  704. return ret;
  705. }
  706. static int ov02a10_probe(struct i2c_client *client)
  707. {
  708. struct device *dev = &client->dev;
  709. struct ov02a10 *ov02a10;
  710. unsigned int i;
  711. unsigned int rotation;
  712. int ret;
  713. ov02a10 = devm_kzalloc(dev, sizeof(*ov02a10), GFP_KERNEL);
  714. if (!ov02a10)
  715. return -ENOMEM;
  716. ret = ov02a10_check_hwcfg(dev, ov02a10);
  717. if (ret)
  718. return dev_err_probe(dev, ret,
  719. "failed to check HW configuration\n");
  720. v4l2_i2c_subdev_init(&ov02a10->subdev, client, &ov02a10_subdev_ops);
  721. ov02a10->subdev.internal_ops = &ov02a10_internal_ops;
  722. ov02a10->mipi_clock_voltage = OV02A10_MIPI_TX_SPEED_DEFAULT;
  723. ov02a10->fmt.code = MEDIA_BUS_FMT_SBGGR10_1X10;
  724. /* Optional indication of physical rotation of sensor */
  725. rotation = 0;
  726. device_property_read_u32(dev, "rotation", &rotation);
  727. if (rotation == 180) {
  728. ov02a10->upside_down = true;
  729. ov02a10->fmt.code = MEDIA_BUS_FMT_SRGGB10_1X10;
  730. }
  731. ov02a10->eclk = devm_clk_get(dev, "eclk");
  732. if (IS_ERR(ov02a10->eclk))
  733. return dev_err_probe(dev, PTR_ERR(ov02a10->eclk),
  734. "failed to get eclk\n");
  735. ret = device_property_read_u32(dev, "clock-frequency",
  736. &ov02a10->eclk_freq);
  737. if (ret < 0)
  738. return dev_err_probe(dev, ret,
  739. "failed to get eclk frequency\n");
  740. ret = clk_set_rate(ov02a10->eclk, ov02a10->eclk_freq);
  741. if (ret < 0)
  742. return dev_err_probe(dev, ret,
  743. "failed to set eclk frequency (24MHz)\n");
  744. if (clk_get_rate(ov02a10->eclk) != OV02A10_ECLK_FREQ)
  745. dev_warn(dev, "eclk mismatched, mode is based on 24MHz\n");
  746. ov02a10->pd_gpio = devm_gpiod_get(dev, "powerdown", GPIOD_OUT_HIGH);
  747. if (IS_ERR(ov02a10->pd_gpio))
  748. return dev_err_probe(dev, PTR_ERR(ov02a10->pd_gpio),
  749. "failed to get powerdown-gpios\n");
  750. ov02a10->rst_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  751. if (IS_ERR(ov02a10->rst_gpio))
  752. return dev_err_probe(dev, PTR_ERR(ov02a10->rst_gpio),
  753. "failed to get reset-gpios\n");
  754. for (i = 0; i < ARRAY_SIZE(ov02a10_supply_names); i++)
  755. ov02a10->supplies[i].supply = ov02a10_supply_names[i];
  756. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ov02a10_supply_names),
  757. ov02a10->supplies);
  758. if (ret)
  759. return dev_err_probe(dev, ret, "failed to get regulators\n");
  760. mutex_init(&ov02a10->mutex);
  761. /* Set default mode */
  762. ov02a10->cur_mode = &supported_modes[0];
  763. ret = ov02a10_initialize_controls(ov02a10);
  764. if (ret) {
  765. dev_err_probe(dev, ret, "failed to initialize controls\n");
  766. goto err_destroy_mutex;
  767. }
  768. /* Initialize subdev */
  769. ov02a10->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  770. ov02a10->subdev.entity.ops = &ov02a10_subdev_entity_ops;
  771. ov02a10->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  772. ov02a10->pad.flags = MEDIA_PAD_FL_SOURCE;
  773. ret = media_entity_pads_init(&ov02a10->subdev.entity, 1, &ov02a10->pad);
  774. if (ret < 0) {
  775. dev_err_probe(dev, ret, "failed to initialize entity pads\n");
  776. goto err_free_handler;
  777. }
  778. pm_runtime_enable(dev);
  779. if (!pm_runtime_enabled(dev)) {
  780. ret = ov02a10_power_on(dev);
  781. if (ret < 0) {
  782. dev_err_probe(dev, ret, "failed to power on\n");
  783. goto err_clean_entity;
  784. }
  785. }
  786. ret = v4l2_async_register_subdev(&ov02a10->subdev);
  787. if (ret) {
  788. dev_err_probe(dev, ret, "failed to register V4L2 subdev\n");
  789. goto err_power_off;
  790. }
  791. return 0;
  792. err_power_off:
  793. if (pm_runtime_enabled(dev))
  794. pm_runtime_disable(dev);
  795. else
  796. ov02a10_power_off(dev);
  797. err_clean_entity:
  798. media_entity_cleanup(&ov02a10->subdev.entity);
  799. err_free_handler:
  800. v4l2_ctrl_handler_free(ov02a10->subdev.ctrl_handler);
  801. err_destroy_mutex:
  802. mutex_destroy(&ov02a10->mutex);
  803. return ret;
  804. }
  805. static void ov02a10_remove(struct i2c_client *client)
  806. {
  807. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  808. struct ov02a10 *ov02a10 = to_ov02a10(sd);
  809. v4l2_async_unregister_subdev(sd);
  810. media_entity_cleanup(&sd->entity);
  811. v4l2_ctrl_handler_free(sd->ctrl_handler);
  812. pm_runtime_disable(&client->dev);
  813. if (!pm_runtime_status_suspended(&client->dev))
  814. ov02a10_power_off(&client->dev);
  815. pm_runtime_set_suspended(&client->dev);
  816. mutex_destroy(&ov02a10->mutex);
  817. }
  818. static const struct of_device_id ov02a10_of_match[] = {
  819. { .compatible = "ovti,ov02a10" },
  820. {}
  821. };
  822. MODULE_DEVICE_TABLE(of, ov02a10_of_match);
  823. static struct i2c_driver ov02a10_i2c_driver = {
  824. .driver = {
  825. .name = "ov02a10",
  826. .pm = &ov02a10_pm_ops,
  827. .of_match_table = ov02a10_of_match,
  828. },
  829. .probe = ov02a10_probe,
  830. .remove = ov02a10_remove,
  831. };
  832. module_i2c_driver(ov02a10_i2c_driver);
  833. MODULE_AUTHOR("Dongchun Zhu <dongchun.zhu@mediatek.com>");
  834. MODULE_DESCRIPTION("OmniVision OV02A10 sensor driver");
  835. MODULE_LICENSE("GPL v2");