ov02e10.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2023 Intel Corporation.
  3. #include <linux/acpi.h>
  4. #include <linux/clk.h>
  5. #include <linux/delay.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/regmap.h>
  11. #include <media/v4l2-cci.h>
  12. #include <media/v4l2-ctrls.h>
  13. #include <media/v4l2-device.h>
  14. #include <media/v4l2-fwnode.h>
  15. #define OV02E10_LINK_FREQ_360MHZ 360000000ULL
  16. #define OV02E10_SCLK 36000000LL
  17. #define OV02E10_MCLK 19200000
  18. #define OV02E10_DATA_LANES 2
  19. #define OV02E10_RGB_DEPTH 10
  20. #define OV02E10_REG_PAGE_FLAG CCI_REG8(0xfd)
  21. #define OV02E10_PAGE_0 0x0
  22. #define OV02E10_PAGE_1 0x1
  23. #define OV02E10_PAGE_2 0x2
  24. #define OV02E10_PAGE_3 0x3
  25. #define OV02E10_PAGE_5 0x4
  26. #define OV02E10_PAGE_7 0x5
  27. #define OV02E10_PAGE_8 0x6
  28. #define OV02E10_PAGE_9 0xF
  29. #define OV02E10_PAGE_D 0x8
  30. #define OV02E10_PAGE_E 0x9
  31. #define OV02E10_PAGE_F 0xA
  32. #define OV02E10_REG_CHIP_ID CCI_REG32(0x00)
  33. #define OV02E10_CHIP_ID 0x45025610
  34. /* Horizontal and vertical flip */
  35. #define OV02E10_REG_ORIENTATION CCI_REG8(0x32)
  36. /* vertical-timings from sensor */
  37. #define OV02E10_REG_VTS CCI_REG16(0x35)
  38. #define OV02E10_VTS_DEF 2244
  39. #define OV02E10_VTS_MIN 2244
  40. #define OV02E10_VTS_MAX 0x7fff
  41. /* horizontal-timings from sensor */
  42. #define OV02E10_REG_HTS CCI_REG16(0x37)
  43. /* Exposure controls from sensor */
  44. #define OV02E10_REG_EXPOSURE CCI_REG16(0x03)
  45. #define OV02E10_EXPOSURE_MIN 1
  46. #define OV02E10_EXPOSURE_MAX_MARGIN 2
  47. #define OV02E10_EXPOSURE_STEP 1
  48. /* Analog gain controls from sensor */
  49. #define OV02E10_REG_ANALOG_GAIN CCI_REG8(0x24)
  50. #define OV02E10_ANAL_GAIN_MIN 0x10
  51. #define OV02E10_ANAL_GAIN_MAX 0xf8
  52. #define OV02E10_ANAL_GAIN_STEP 1
  53. /* Digital gain controls from sensor */
  54. #define OV02E10_REG_DIGITAL_GAIN CCI_REG16(0x21)
  55. #define OV02E10_DGTL_GAIN_MIN 256
  56. #define OV02E10_DGTL_GAIN_MAX 1020
  57. #define OV02E10_DGTL_GAIN_STEP 1
  58. #define OV02E10_DGTL_GAIN_DEFAULT 256
  59. /* Register update control */
  60. #define OV02E10_REG_COMMAND_UPDATE CCI_REG8(0xE7)
  61. #define OV02E10_COMMAND_UPDATE 0x00
  62. #define OV02E10_COMMAND_HOLD 0x01
  63. /* Test Pattern Control */
  64. #define OV02E10_REG_TEST_PATTERN CCI_REG8(0x12)
  65. #define OV02E10_TEST_PATTERN_ENABLE BIT(0)
  66. #define OV02E10_TEST_PATTERN_BAR_SHIFT 1
  67. struct reg_sequence_list {
  68. u32 num_regs;
  69. const struct reg_sequence *regs;
  70. };
  71. struct ov02e10_mode {
  72. /* Frame width in pixels */
  73. u32 width;
  74. /* Frame height in pixels */
  75. u32 height;
  76. /* Horizontal timining size */
  77. u32 hts;
  78. /* Default vertical timing */
  79. u32 vts_def;
  80. /* Min vertical timining size */
  81. u32 vts_min;
  82. /* Sensor register settings for this resolution */
  83. const struct reg_sequence_list reg_list;
  84. };
  85. static const struct reg_sequence mode_1928x1088_30fps_2lane[] = {
  86. { 0xfd, 0x00 },
  87. { 0x20, 0x00 },
  88. { 0x20, 0x0b },
  89. { 0x21, 0x02 },
  90. { 0x10, 0x23 },
  91. { 0xc5, 0x04 },
  92. { 0x21, 0x00 },
  93. { 0x14, 0x96 },
  94. { 0x17, 0x01 },
  95. { 0xfd, 0x01 },
  96. { 0x03, 0x00 },
  97. { 0x04, 0x04 },
  98. { 0x05, 0x04 },
  99. { 0x06, 0x62 },
  100. { 0x07, 0x01 },
  101. { 0x22, 0x80 },
  102. { 0x24, 0xff },
  103. { 0x40, 0xc6 },
  104. { 0x41, 0x18 },
  105. { 0x45, 0x3f },
  106. { 0x48, 0x0c },
  107. { 0x4c, 0x08 },
  108. { 0x51, 0x12 },
  109. { 0x52, 0x10 },
  110. { 0x57, 0x98 },
  111. { 0x59, 0x06 },
  112. { 0x5a, 0x04 },
  113. { 0x5c, 0x38 },
  114. { 0x5e, 0x10 },
  115. { 0x67, 0x11 },
  116. { 0x7b, 0x04 },
  117. { 0x81, 0x12 },
  118. { 0x90, 0x51 },
  119. { 0x91, 0x09 },
  120. { 0x92, 0x21 },
  121. { 0x93, 0x28 },
  122. { 0x95, 0x54 },
  123. { 0x9d, 0x20 },
  124. { 0x9e, 0x04 },
  125. { 0xb1, 0x9a },
  126. { 0xb2, 0x86 },
  127. { 0xb6, 0x3f },
  128. { 0xb9, 0x30 },
  129. { 0xc1, 0x01 },
  130. { 0xc5, 0xa0 },
  131. { 0xc6, 0x73 },
  132. { 0xc7, 0x04 },
  133. { 0xc8, 0x25 },
  134. { 0xc9, 0x05 },
  135. { 0xca, 0x28 },
  136. { 0xcb, 0x00 },
  137. { 0xcf, 0x16 },
  138. { 0xd2, 0xd0 },
  139. { 0xd7, 0x3f },
  140. { 0xd8, 0x40 },
  141. { 0xd9, 0x40 },
  142. { 0xda, 0x44 },
  143. { 0xdb, 0x3d },
  144. { 0xdc, 0x3d },
  145. { 0xdd, 0x3d },
  146. { 0xde, 0x3d },
  147. { 0xdf, 0xf0 },
  148. { 0xea, 0x0f },
  149. { 0xeb, 0x04 },
  150. { 0xec, 0x29 },
  151. { 0xee, 0x47 },
  152. { 0xfd, 0x01 },
  153. { 0x31, 0x01 },
  154. { 0x27, 0x00 },
  155. { 0x2f, 0x41 },
  156. { 0xfd, 0x02 },
  157. { 0xa1, 0x01 },
  158. { 0xfd, 0x02 },
  159. { 0x9a, 0x03 },
  160. { 0xfd, 0x03 },
  161. { 0x9d, 0x0f },
  162. { 0xfd, 0x07 },
  163. { 0x42, 0x00 },
  164. { 0x43, 0xad },
  165. { 0x44, 0x00 },
  166. { 0x45, 0xa8 },
  167. { 0x46, 0x00 },
  168. { 0x47, 0xa8 },
  169. { 0x48, 0x00 },
  170. { 0x49, 0xad },
  171. { 0xfd, 0x00 },
  172. { 0xc4, 0x01 },
  173. { 0xfd, 0x01 },
  174. { 0x33, 0x03 },
  175. { 0xfd, 0x00 },
  176. { 0x20, 0x1f },
  177. };
  178. static const char *const ov02e10_test_pattern_menu[] = {
  179. "Disabled",
  180. "Color Bar",
  181. };
  182. static const s64 link_freq_menu_items[] = {
  183. OV02E10_LINK_FREQ_360MHZ,
  184. };
  185. static const struct ov02e10_mode supported_modes[] = {
  186. {
  187. .width = 1928,
  188. .height = 1088,
  189. .hts = 534,
  190. .vts_def = 2244,
  191. .vts_min = 2244,
  192. .reg_list = {
  193. .num_regs = ARRAY_SIZE(mode_1928x1088_30fps_2lane),
  194. .regs = mode_1928x1088_30fps_2lane,
  195. },
  196. },
  197. };
  198. static const char * const ov02e10_supply_names[] = {
  199. "dovdd", /* Digital I/O power */
  200. "avdd", /* Analog power */
  201. "dvdd", /* Digital core power */
  202. };
  203. struct ov02e10 {
  204. struct device *dev;
  205. struct regmap *regmap;
  206. struct v4l2_subdev sd;
  207. struct media_pad pad;
  208. struct v4l2_ctrl_handler ctrl_handler;
  209. /* V4L2 Controls */
  210. struct v4l2_ctrl *link_freq;
  211. struct v4l2_ctrl *pixel_rate;
  212. struct v4l2_ctrl *vblank;
  213. struct v4l2_ctrl *hblank;
  214. struct v4l2_ctrl *exposure;
  215. struct v4l2_ctrl *vflip;
  216. struct v4l2_ctrl *hflip;
  217. struct clk *img_clk;
  218. struct regulator_bulk_data supplies[ARRAY_SIZE(ov02e10_supply_names)];
  219. struct gpio_desc *reset;
  220. /* Current mode */
  221. const struct ov02e10_mode *cur_mode;
  222. /* MIPI lanes info */
  223. u32 link_freq_index;
  224. u8 mipi_lanes;
  225. };
  226. static inline struct ov02e10 *to_ov02e10(struct v4l2_subdev *subdev)
  227. {
  228. return container_of(subdev, struct ov02e10, sd);
  229. }
  230. static u64 to_pixel_rate(u32 f_index)
  231. {
  232. u64 pixel_rate = link_freq_menu_items[f_index] * 2 * OV02E10_DATA_LANES;
  233. do_div(pixel_rate, OV02E10_RGB_DEPTH);
  234. return pixel_rate;
  235. }
  236. static u64 to_pixels_per_line(u32 hts, u32 f_index)
  237. {
  238. u64 ppl = hts * to_pixel_rate(f_index);
  239. do_div(ppl, OV02E10_SCLK);
  240. return ppl;
  241. }
  242. static void ov02e10_test_pattern(struct ov02e10 *ov02e10, u32 pattern, int *pret)
  243. {
  244. if (pattern)
  245. pattern = pattern << OV02E10_TEST_PATTERN_BAR_SHIFT |
  246. OV02E10_TEST_PATTERN_ENABLE;
  247. cci_write(ov02e10->regmap, OV02E10_REG_TEST_PATTERN, pattern, pret);
  248. }
  249. static int ov02e10_set_ctrl(struct v4l2_ctrl *ctrl)
  250. {
  251. struct ov02e10 *ov02e10 = container_of(ctrl->handler,
  252. struct ov02e10, ctrl_handler);
  253. s64 exposure_max;
  254. int ret;
  255. /* Propagate change of current control to all related controls */
  256. if (ctrl->id == V4L2_CID_VBLANK) {
  257. /* Update max exposure while meeting expected vblanking */
  258. exposure_max = ov02e10->cur_mode->height + ctrl->val -
  259. OV02E10_EXPOSURE_MAX_MARGIN;
  260. ret = __v4l2_ctrl_modify_range(ov02e10->exposure,
  261. ov02e10->exposure->minimum,
  262. exposure_max,
  263. ov02e10->exposure->step,
  264. exposure_max);
  265. if (ret)
  266. return ret;
  267. }
  268. /* V4L2 controls values will be applied only when power is already up */
  269. if (!pm_runtime_get_if_in_use(ov02e10->dev))
  270. return 0;
  271. ret = cci_write(ov02e10->regmap, OV02E10_REG_COMMAND_UPDATE,
  272. OV02E10_COMMAND_HOLD, NULL);
  273. switch (ctrl->id) {
  274. case V4L2_CID_ANALOGUE_GAIN:
  275. cci_write(ov02e10->regmap, OV02E10_REG_PAGE_FLAG,
  276. OV02E10_PAGE_1, &ret);
  277. cci_write(ov02e10->regmap, OV02E10_REG_ANALOG_GAIN,
  278. ctrl->val, &ret);
  279. break;
  280. case V4L2_CID_DIGITAL_GAIN:
  281. cci_write(ov02e10->regmap, OV02E10_REG_PAGE_FLAG,
  282. OV02E10_PAGE_1, &ret);
  283. cci_write(ov02e10->regmap, OV02E10_REG_DIGITAL_GAIN,
  284. ctrl->val, &ret);
  285. break;
  286. case V4L2_CID_EXPOSURE:
  287. cci_write(ov02e10->regmap, OV02E10_REG_PAGE_FLAG,
  288. OV02E10_PAGE_1, &ret);
  289. cci_write(ov02e10->regmap, OV02E10_REG_EXPOSURE,
  290. ctrl->val, &ret);
  291. break;
  292. case V4L2_CID_HFLIP:
  293. case V4L2_CID_VFLIP:
  294. cci_write(ov02e10->regmap, OV02E10_REG_PAGE_FLAG,
  295. OV02E10_PAGE_1, &ret);
  296. cci_write(ov02e10->regmap, OV02E10_REG_ORIENTATION,
  297. ov02e10->hflip->val | ov02e10->vflip->val << 1, &ret);
  298. break;
  299. case V4L2_CID_VBLANK:
  300. cci_write(ov02e10->regmap, OV02E10_REG_PAGE_FLAG,
  301. OV02E10_PAGE_1, &ret);
  302. cci_write(ov02e10->regmap, OV02E10_REG_VTS,
  303. ov02e10->cur_mode->height + ctrl->val, &ret);
  304. break;
  305. case V4L2_CID_TEST_PATTERN:
  306. cci_write(ov02e10->regmap, OV02E10_REG_PAGE_FLAG,
  307. OV02E10_PAGE_1, &ret);
  308. ov02e10_test_pattern(ov02e10, ctrl->val, &ret);
  309. break;
  310. default:
  311. ret = -EINVAL;
  312. break;
  313. }
  314. cci_write(ov02e10->regmap, OV02E10_REG_COMMAND_UPDATE,
  315. OV02E10_COMMAND_UPDATE, &ret);
  316. pm_runtime_put(ov02e10->dev);
  317. return ret;
  318. }
  319. static const struct v4l2_ctrl_ops ov02e10_ctrl_ops = {
  320. .s_ctrl = ov02e10_set_ctrl,
  321. };
  322. static int ov02e10_init_controls(struct ov02e10 *ov02e10)
  323. {
  324. struct v4l2_ctrl_handler *ctrl_hdlr = &ov02e10->ctrl_handler;
  325. const struct ov02e10_mode *mode = ov02e10->cur_mode;
  326. u32 vblank_min, vblank_max, vblank_def;
  327. struct v4l2_fwnode_device_properties props;
  328. s64 exposure_max, h_blank, pixel_rate;
  329. int ret;
  330. v4l2_ctrl_handler_init(ctrl_hdlr, 12);
  331. ov02e10->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
  332. &ov02e10_ctrl_ops,
  333. V4L2_CID_LINK_FREQ,
  334. ov02e10->link_freq_index,
  335. 0, link_freq_menu_items);
  336. if (ov02e10->link_freq)
  337. ov02e10->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  338. pixel_rate = to_pixel_rate(ov02e10->link_freq_index);
  339. ov02e10->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov02e10_ctrl_ops,
  340. V4L2_CID_PIXEL_RATE, 0,
  341. pixel_rate, 1, pixel_rate);
  342. vblank_min = mode->vts_min - mode->height;
  343. vblank_max = OV02E10_VTS_MAX - mode->height;
  344. vblank_def = mode->vts_def - mode->height;
  345. ov02e10->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov02e10_ctrl_ops,
  346. V4L2_CID_VBLANK, vblank_min,
  347. vblank_max, 1, vblank_def);
  348. h_blank = mode->hts - mode->width;
  349. ov02e10->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov02e10_ctrl_ops,
  350. V4L2_CID_HBLANK, h_blank, h_blank,
  351. 1, h_blank);
  352. if (ov02e10->hblank)
  353. ov02e10->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  354. v4l2_ctrl_new_std(ctrl_hdlr, &ov02e10_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
  355. OV02E10_ANAL_GAIN_MIN, OV02E10_ANAL_GAIN_MAX,
  356. OV02E10_ANAL_GAIN_STEP, OV02E10_ANAL_GAIN_MIN);
  357. v4l2_ctrl_new_std(ctrl_hdlr, &ov02e10_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
  358. OV02E10_DGTL_GAIN_MIN, OV02E10_DGTL_GAIN_MAX,
  359. OV02E10_DGTL_GAIN_STEP, OV02E10_DGTL_GAIN_DEFAULT);
  360. exposure_max = mode->vts_def - OV02E10_EXPOSURE_MAX_MARGIN;
  361. ov02e10->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov02e10_ctrl_ops,
  362. V4L2_CID_EXPOSURE,
  363. OV02E10_EXPOSURE_MIN,
  364. exposure_max,
  365. OV02E10_EXPOSURE_STEP,
  366. exposure_max);
  367. ov02e10->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &ov02e10_ctrl_ops,
  368. V4L2_CID_HFLIP, 0, 1, 1, 0);
  369. if (ov02e10->hflip)
  370. ov02e10->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
  371. ov02e10->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &ov02e10_ctrl_ops,
  372. V4L2_CID_VFLIP, 0, 1, 1, 0);
  373. if (ov02e10->vflip)
  374. ov02e10->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
  375. v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov02e10_ctrl_ops,
  376. V4L2_CID_TEST_PATTERN,
  377. ARRAY_SIZE(ov02e10_test_pattern_menu) - 1,
  378. 0, 0, ov02e10_test_pattern_menu);
  379. ret = v4l2_fwnode_device_parse(ov02e10->dev, &props);
  380. if (ret)
  381. return ret;
  382. v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov02e10_ctrl_ops, &props);
  383. if (ctrl_hdlr->error)
  384. return ctrl_hdlr->error;
  385. ov02e10->sd.ctrl_handler = ctrl_hdlr;
  386. return 0;
  387. }
  388. static void ov02e10_update_pad_format(const struct ov02e10_mode *mode,
  389. struct v4l2_mbus_framefmt *fmt)
  390. {
  391. fmt->width = mode->width;
  392. fmt->height = mode->height;
  393. fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  394. fmt->field = V4L2_FIELD_NONE;
  395. }
  396. static int ov02e10_set_stream_mode(struct ov02e10 *ov02e10, u8 val)
  397. {
  398. int ret = 0;
  399. cci_write(ov02e10->regmap, OV02E10_REG_PAGE_FLAG, OV02E10_PAGE_0, &ret);
  400. cci_write(ov02e10->regmap, CCI_REG8(0xa0), val, &ret);
  401. cci_write(ov02e10->regmap, OV02E10_REG_PAGE_FLAG, OV02E10_PAGE_1, &ret);
  402. cci_write(ov02e10->regmap, CCI_REG8(0x01), 0x02, &ret);
  403. return ret;
  404. }
  405. static int ov02e10_enable_streams(struct v4l2_subdev *sd,
  406. struct v4l2_subdev_state *state,
  407. u32 pad, u64 streams_mask)
  408. {
  409. struct ov02e10 *ov02e10 = to_ov02e10(sd);
  410. const struct reg_sequence_list *reg_list;
  411. int ret;
  412. ret = pm_runtime_resume_and_get(ov02e10->dev);
  413. if (ret)
  414. return ret;
  415. reg_list = &ov02e10->cur_mode->reg_list;
  416. ret = regmap_multi_reg_write(ov02e10->regmap, reg_list->regs,
  417. reg_list->num_regs);
  418. if (ret) {
  419. dev_err(ov02e10->dev, "failed to set mode\n");
  420. goto out;
  421. }
  422. ret = __v4l2_ctrl_handler_setup(ov02e10->sd.ctrl_handler);
  423. if (ret)
  424. goto out;
  425. ret = ov02e10_set_stream_mode(ov02e10, 1);
  426. out:
  427. if (ret)
  428. pm_runtime_put(ov02e10->dev);
  429. return ret;
  430. }
  431. static int ov02e10_disable_streams(struct v4l2_subdev *sd,
  432. struct v4l2_subdev_state *state,
  433. u32 pad, u64 streams_mask)
  434. {
  435. struct ov02e10 *ov02e10 = to_ov02e10(sd);
  436. ov02e10_set_stream_mode(ov02e10, 0);
  437. pm_runtime_put(ov02e10->dev);
  438. return 0;
  439. }
  440. static int ov02e10_get_pm_resources(struct device *dev)
  441. {
  442. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  443. struct ov02e10 *ov02e10 = to_ov02e10(sd);
  444. int i;
  445. ov02e10->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  446. if (IS_ERR(ov02e10->reset))
  447. return dev_err_probe(dev, PTR_ERR(ov02e10->reset),
  448. "failed to get reset gpio\n");
  449. for (i = 0; i < ARRAY_SIZE(ov02e10_supply_names); i++)
  450. ov02e10->supplies[i].supply = ov02e10_supply_names[i];
  451. return devm_regulator_bulk_get(dev, ARRAY_SIZE(ov02e10_supply_names),
  452. ov02e10->supplies);
  453. }
  454. static int ov02e10_power_off(struct device *dev)
  455. {
  456. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  457. struct ov02e10 *ov02e10 = to_ov02e10(sd);
  458. if (ov02e10->reset)
  459. gpiod_set_value_cansleep(ov02e10->reset, 1);
  460. regulator_bulk_disable(ARRAY_SIZE(ov02e10_supply_names),
  461. ov02e10->supplies);
  462. clk_disable_unprepare(ov02e10->img_clk);
  463. return 0;
  464. }
  465. static int ov02e10_power_on(struct device *dev)
  466. {
  467. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  468. struct ov02e10 *ov02e10 = to_ov02e10(sd);
  469. int ret;
  470. ret = clk_prepare_enable(ov02e10->img_clk);
  471. if (ret < 0) {
  472. dev_err(dev, "failed to enable imaging clock: %d\n", ret);
  473. return ret;
  474. }
  475. ret = regulator_bulk_enable(ARRAY_SIZE(ov02e10_supply_names),
  476. ov02e10->supplies);
  477. if (ret < 0) {
  478. dev_err(dev, "failed to enable regulators\n");
  479. goto disable_clk;
  480. }
  481. if (ov02e10->reset) {
  482. usleep_range(5000, 5100);
  483. gpiod_set_value_cansleep(ov02e10->reset, 0);
  484. usleep_range(8000, 8100);
  485. }
  486. return 0;
  487. disable_clk:
  488. clk_disable_unprepare(ov02e10->img_clk);
  489. return ret;
  490. }
  491. static int ov02e10_set_format(struct v4l2_subdev *sd,
  492. struct v4l2_subdev_state *sd_state,
  493. struct v4l2_subdev_format *fmt)
  494. {
  495. struct ov02e10 *ov02e10 = to_ov02e10(sd);
  496. const struct ov02e10_mode *mode;
  497. s32 vblank_def, h_blank;
  498. int ret = 0;
  499. mode = v4l2_find_nearest_size(supported_modes,
  500. ARRAY_SIZE(supported_modes),
  501. width, height, fmt->format.width,
  502. fmt->format.height);
  503. ov02e10_update_pad_format(mode, &fmt->format);
  504. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  505. *v4l2_subdev_state_get_format(sd_state, fmt->pad) = fmt->format;
  506. } else {
  507. ov02e10->cur_mode = mode;
  508. ret = __v4l2_ctrl_s_ctrl(ov02e10->link_freq,
  509. ov02e10->link_freq_index);
  510. if (ret)
  511. return ret;
  512. ret = __v4l2_ctrl_s_ctrl_int64(ov02e10->pixel_rate,
  513. to_pixel_rate(ov02e10->link_freq_index));
  514. if (ret)
  515. return ret;
  516. /* Update limits and set FPS to default */
  517. vblank_def = mode->vts_def - mode->height;
  518. ret = __v4l2_ctrl_modify_range(ov02e10->vblank,
  519. mode->vts_min - mode->height,
  520. OV02E10_VTS_MAX - mode->height,
  521. 1, vblank_def);
  522. if (ret)
  523. return ret;
  524. ret = __v4l2_ctrl_s_ctrl(ov02e10->vblank, vblank_def);
  525. if (ret)
  526. return ret;
  527. h_blank = to_pixels_per_line(mode->hts, ov02e10->link_freq_index);
  528. h_blank -= mode->width;
  529. ret = __v4l2_ctrl_modify_range(ov02e10->hblank, h_blank,
  530. h_blank, 1, h_blank);
  531. }
  532. return ret;
  533. }
  534. static int ov02e10_get_format(struct v4l2_subdev *sd,
  535. struct v4l2_subdev_state *sd_state,
  536. struct v4l2_subdev_format *fmt)
  537. {
  538. struct ov02e10 *ov02e10 = to_ov02e10(sd);
  539. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
  540. fmt->format = *v4l2_subdev_state_get_format(sd_state, fmt->pad);
  541. else
  542. ov02e10_update_pad_format(ov02e10->cur_mode, &fmt->format);
  543. return 0;
  544. }
  545. static int ov02e10_enum_mbus_code(struct v4l2_subdev *sd,
  546. struct v4l2_subdev_state *sd_state,
  547. struct v4l2_subdev_mbus_code_enum *code)
  548. {
  549. if (code->index > 0)
  550. return -EINVAL;
  551. code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  552. return 0;
  553. }
  554. static int ov02e10_enum_frame_size(struct v4l2_subdev *sd,
  555. struct v4l2_subdev_state *sd_state,
  556. struct v4l2_subdev_frame_size_enum *fse)
  557. {
  558. if (fse->index >= ARRAY_SIZE(supported_modes))
  559. return -EINVAL;
  560. if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
  561. return -EINVAL;
  562. fse->min_width = supported_modes[fse->index].width;
  563. fse->max_width = fse->min_width;
  564. fse->min_height = supported_modes[fse->index].height;
  565. fse->max_height = fse->min_height;
  566. return 0;
  567. }
  568. static int ov02e10_init_state(struct v4l2_subdev *sd,
  569. struct v4l2_subdev_state *sd_state)
  570. {
  571. ov02e10_update_pad_format(&supported_modes[0],
  572. v4l2_subdev_state_get_format(sd_state, 0));
  573. return 0;
  574. }
  575. static const struct v4l2_subdev_video_ops ov02e10_video_ops = {
  576. .s_stream = v4l2_subdev_s_stream_helper,
  577. };
  578. static const struct v4l2_subdev_pad_ops ov02e10_pad_ops = {
  579. .set_fmt = ov02e10_set_format,
  580. .get_fmt = ov02e10_get_format,
  581. .enum_mbus_code = ov02e10_enum_mbus_code,
  582. .enum_frame_size = ov02e10_enum_frame_size,
  583. .enable_streams = ov02e10_enable_streams,
  584. .disable_streams = ov02e10_disable_streams,
  585. };
  586. static const struct v4l2_subdev_ops ov02e10_subdev_ops = {
  587. .video = &ov02e10_video_ops,
  588. .pad = &ov02e10_pad_ops,
  589. };
  590. static const struct media_entity_operations ov02e10_subdev_entity_ops = {
  591. .link_validate = v4l2_subdev_link_validate,
  592. };
  593. static const struct v4l2_subdev_internal_ops ov02e10_internal_ops = {
  594. .init_state = ov02e10_init_state,
  595. };
  596. static int ov02e10_identify_module(struct ov02e10 *ov02e10)
  597. {
  598. int ret;
  599. u64 val;
  600. ret = cci_write(ov02e10->regmap, OV02E10_REG_PAGE_FLAG,
  601. OV02E10_PAGE_0, NULL);
  602. cci_read(ov02e10->regmap, OV02E10_REG_CHIP_ID, &val, &ret);
  603. if (ret)
  604. return ret;
  605. if (val != OV02E10_CHIP_ID) {
  606. dev_err(ov02e10->dev, "chip id mismatch: %x!=%x\n",
  607. OV02E10_CHIP_ID, (u32)val);
  608. return -ENXIO;
  609. }
  610. return 0;
  611. }
  612. static int ov02e10_check_hwcfg(struct ov02e10 *ov02e10)
  613. {
  614. struct v4l2_fwnode_endpoint bus_cfg = {
  615. .bus_type = V4L2_MBUS_CSI2_DPHY
  616. };
  617. struct device *dev = ov02e10->dev;
  618. struct fwnode_handle *ep;
  619. struct fwnode_handle *fwnode = dev_fwnode(dev);
  620. unsigned long link_freq_bitmap;
  621. int ret;
  622. ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
  623. if (!ep)
  624. return dev_err_probe(dev, -EPROBE_DEFER,
  625. "waiting for fwnode graph endpoint\n");
  626. ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
  627. fwnode_handle_put(ep);
  628. if (ret)
  629. return dev_err_probe(dev, ret, "parsing endpoint failed\n");
  630. if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV02E10_DATA_LANES) {
  631. dev_err(dev, "number of CSI2 data lanes %d is not supported\n",
  632. bus_cfg.bus.mipi_csi2.num_data_lanes);
  633. ret = -EINVAL;
  634. goto out_err;
  635. }
  636. if (!bus_cfg.nr_of_link_frequencies) {
  637. dev_err(dev, "no link frequencies defined\n");
  638. ret = -EINVAL;
  639. goto out_err;
  640. }
  641. ret = v4l2_link_freq_to_bitmap(dev, bus_cfg.link_frequencies,
  642. bus_cfg.nr_of_link_frequencies,
  643. link_freq_menu_items,
  644. ARRAY_SIZE(link_freq_menu_items),
  645. &link_freq_bitmap);
  646. if (ret)
  647. goto out_err;
  648. /* v4l2_link_freq_to_bitmap() guarantees at least 1 bit is set */
  649. ov02e10->link_freq_index = ffs(link_freq_bitmap) - 1;
  650. ov02e10->mipi_lanes = bus_cfg.bus.mipi_csi2.num_data_lanes;
  651. out_err:
  652. v4l2_fwnode_endpoint_free(&bus_cfg);
  653. return ret;
  654. }
  655. static void ov02e10_remove(struct i2c_client *client)
  656. {
  657. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  658. struct ov02e10 *ov02e10 = to_ov02e10(sd);
  659. v4l2_async_unregister_subdev(sd);
  660. v4l2_subdev_cleanup(sd);
  661. media_entity_cleanup(&sd->entity);
  662. v4l2_ctrl_handler_free(sd->ctrl_handler);
  663. pm_runtime_disable(ov02e10->dev);
  664. if (!pm_runtime_status_suspended(ov02e10->dev)) {
  665. ov02e10_power_off(ov02e10->dev);
  666. pm_runtime_set_suspended(ov02e10->dev);
  667. }
  668. }
  669. static int ov02e10_probe(struct i2c_client *client)
  670. {
  671. struct ov02e10 *ov02e10;
  672. unsigned long freq;
  673. int ret;
  674. ov02e10 = devm_kzalloc(&client->dev, sizeof(*ov02e10), GFP_KERNEL);
  675. if (!ov02e10)
  676. return -ENOMEM;
  677. ov02e10->dev = &client->dev;
  678. ov02e10->img_clk = devm_v4l2_sensor_clk_get(ov02e10->dev, NULL);
  679. if (IS_ERR(ov02e10->img_clk))
  680. return dev_err_probe(ov02e10->dev, PTR_ERR(ov02e10->img_clk),
  681. "failed to get imaging clock\n");
  682. freq = clk_get_rate(ov02e10->img_clk);
  683. if (freq != OV02E10_MCLK)
  684. return dev_err_probe(ov02e10->dev, -EINVAL,
  685. "external clock %lu is not supported",
  686. freq);
  687. v4l2_i2c_subdev_init(&ov02e10->sd, client, &ov02e10_subdev_ops);
  688. /* Check HW config */
  689. ret = ov02e10_check_hwcfg(ov02e10);
  690. if (ret)
  691. return ret;
  692. /* Initialize subdev */
  693. ov02e10->regmap = devm_cci_regmap_init_i2c(client, 8);
  694. if (IS_ERR(ov02e10->regmap))
  695. return PTR_ERR(ov02e10->regmap);
  696. ret = ov02e10_get_pm_resources(ov02e10->dev);
  697. if (ret)
  698. return ret;
  699. ret = ov02e10_power_on(ov02e10->dev);
  700. if (ret) {
  701. dev_err_probe(ov02e10->dev, ret, "failed to power on\n");
  702. return ret;
  703. }
  704. /* Check module identity */
  705. ret = ov02e10_identify_module(ov02e10);
  706. if (ret) {
  707. dev_err(ov02e10->dev, "failed to find sensor: %d\n", ret);
  708. goto probe_error_power_off;
  709. }
  710. ov02e10->cur_mode = &supported_modes[0];
  711. ret = ov02e10_init_controls(ov02e10);
  712. if (ret) {
  713. dev_err(ov02e10->dev, "failed to init controls: %d\n", ret);
  714. goto probe_error_v4l2_ctrl_handler_free;
  715. }
  716. /* Initialize subdev */
  717. ov02e10->sd.internal_ops = &ov02e10_internal_ops;
  718. ov02e10->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  719. ov02e10->sd.entity.ops = &ov02e10_subdev_entity_ops;
  720. ov02e10->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  721. /* Initialize source pad */
  722. ov02e10->pad.flags = MEDIA_PAD_FL_SOURCE;
  723. ret = media_entity_pads_init(&ov02e10->sd.entity, 1, &ov02e10->pad);
  724. if (ret) {
  725. dev_err(ov02e10->dev, "failed to init entity pads: %d", ret);
  726. goto probe_error_v4l2_ctrl_handler_free;
  727. }
  728. ov02e10->sd.state_lock = ov02e10->ctrl_handler.lock;
  729. ret = v4l2_subdev_init_finalize(&ov02e10->sd);
  730. if (ret < 0) {
  731. dev_err(ov02e10->dev, "failed to init subdev: %d", ret);
  732. goto probe_error_media_entity_cleanup;
  733. }
  734. pm_runtime_set_active(ov02e10->dev);
  735. pm_runtime_enable(ov02e10->dev);
  736. ret = v4l2_async_register_subdev_sensor(&ov02e10->sd);
  737. if (ret < 0) {
  738. dev_err(ov02e10->dev, "failed to register V4L2 subdev: %d",
  739. ret);
  740. goto probe_error_v4l2_subdev_cleanup;
  741. }
  742. pm_runtime_idle(ov02e10->dev);
  743. return 0;
  744. probe_error_v4l2_subdev_cleanup:
  745. pm_runtime_disable(ov02e10->dev);
  746. pm_runtime_set_suspended(ov02e10->dev);
  747. v4l2_subdev_cleanup(&ov02e10->sd);
  748. probe_error_media_entity_cleanup:
  749. media_entity_cleanup(&ov02e10->sd.entity);
  750. probe_error_v4l2_ctrl_handler_free:
  751. v4l2_ctrl_handler_free(ov02e10->sd.ctrl_handler);
  752. probe_error_power_off:
  753. ov02e10_power_off(ov02e10->dev);
  754. return ret;
  755. }
  756. static DEFINE_RUNTIME_DEV_PM_OPS(ov02e10_pm_ops, ov02e10_power_off,
  757. ov02e10_power_on, NULL);
  758. static const struct acpi_device_id ov02e10_acpi_ids[] = {
  759. { "OVTI02E1" },
  760. { /* sentinel */ }
  761. };
  762. MODULE_DEVICE_TABLE(acpi, ov02e10_acpi_ids);
  763. static const struct of_device_id ov02e10_of_match[] = {
  764. { .compatible = "ovti,ov02e10" },
  765. { /* sentinel */ }
  766. };
  767. MODULE_DEVICE_TABLE(of, ov02e10_of_match);
  768. static struct i2c_driver ov02e10_i2c_driver = {
  769. .driver = {
  770. .name = "ov02e10",
  771. .pm = pm_sleep_ptr(&ov02e10_pm_ops),
  772. .acpi_match_table = ov02e10_acpi_ids,
  773. .of_match_table = ov02e10_of_match,
  774. },
  775. .probe = ov02e10_probe,
  776. .remove = ov02e10_remove,
  777. };
  778. module_i2c_driver(ov02e10_i2c_driver);
  779. MODULE_AUTHOR("Jingjing Xiong");
  780. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  781. MODULE_AUTHOR("Alan Stern <stern@rowland.harvard.edu>");
  782. MODULE_AUTHOR("Bryan O'Donoghue <bryan.odonoghue@linaro.org>");
  783. MODULE_DESCRIPTION("OmniVision OV02E10 sensor driver");
  784. MODULE_LICENSE("GPL");