imx290.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Sony IMX290 CMOS Image Sensor Driver
  4. *
  5. * Copyright (C) 2019 FRAMOS GmbH.
  6. *
  7. * Copyright (C) 2019 Linaro Ltd.
  8. * Author: Manivannan Sadhasivam <[email protected]>
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/i2c.h>
  14. #include <linux/module.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/regmap.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <media/media-entity.h>
  19. #include <media/v4l2-ctrls.h>
  20. #include <media/v4l2-device.h>
  21. #include <media/v4l2-fwnode.h>
  22. #include <media/v4l2-subdev.h>
  23. #define IMX290_STANDBY 0x3000
  24. #define IMX290_REGHOLD 0x3001
  25. #define IMX290_XMSTA 0x3002
  26. #define IMX290_FR_FDG_SEL 0x3009
  27. #define IMX290_BLKLEVEL_LOW 0x300a
  28. #define IMX290_BLKLEVEL_HIGH 0x300b
  29. #define IMX290_GAIN 0x3014
  30. #define IMX290_HMAX_LOW 0x301c
  31. #define IMX290_HMAX_HIGH 0x301d
  32. #define IMX290_PGCTRL 0x308c
  33. #define IMX290_PHY_LANE_NUM 0x3407
  34. #define IMX290_CSI_LANE_MODE 0x3443
  35. #define IMX290_PGCTRL_REGEN BIT(0)
  36. #define IMX290_PGCTRL_THRU BIT(1)
  37. #define IMX290_PGCTRL_MODE(n) ((n) << 4)
  38. static const char * const imx290_supply_name[] = {
  39. "vdda",
  40. "vddd",
  41. "vdddo",
  42. };
  43. #define IMX290_NUM_SUPPLIES ARRAY_SIZE(imx290_supply_name)
  44. struct imx290_regval {
  45. u16 reg;
  46. u8 val;
  47. };
  48. struct imx290_mode {
  49. u32 width;
  50. u32 height;
  51. u32 hmax;
  52. u8 link_freq_index;
  53. const struct imx290_regval *data;
  54. u32 data_size;
  55. };
  56. struct imx290 {
  57. struct device *dev;
  58. struct clk *xclk;
  59. struct regmap *regmap;
  60. u8 nlanes;
  61. u8 bpp;
  62. struct v4l2_subdev sd;
  63. struct media_pad pad;
  64. struct v4l2_mbus_framefmt current_format;
  65. const struct imx290_mode *current_mode;
  66. struct regulator_bulk_data supplies[IMX290_NUM_SUPPLIES];
  67. struct gpio_desc *rst_gpio;
  68. struct v4l2_ctrl_handler ctrls;
  69. struct v4l2_ctrl *link_freq;
  70. struct v4l2_ctrl *pixel_rate;
  71. struct mutex lock;
  72. };
  73. struct imx290_pixfmt {
  74. u32 code;
  75. u8 bpp;
  76. };
  77. static const struct imx290_pixfmt imx290_formats[] = {
  78. { MEDIA_BUS_FMT_SRGGB10_1X10, 10 },
  79. { MEDIA_BUS_FMT_SRGGB12_1X12, 12 },
  80. };
  81. static const struct regmap_config imx290_regmap_config = {
  82. .reg_bits = 16,
  83. .val_bits = 8,
  84. .cache_type = REGCACHE_RBTREE,
  85. };
  86. static const char * const imx290_test_pattern_menu[] = {
  87. "Disabled",
  88. "Sequence Pattern 1",
  89. "Horizontal Color-bar Chart",
  90. "Vertical Color-bar Chart",
  91. "Sequence Pattern 2",
  92. "Gradation Pattern 1",
  93. "Gradation Pattern 2",
  94. "000/555h Toggle Pattern",
  95. };
  96. static const struct imx290_regval imx290_global_init_settings[] = {
  97. { 0x3007, 0x00 },
  98. { 0x3018, 0x65 },
  99. { 0x3019, 0x04 },
  100. { 0x301a, 0x00 },
  101. { 0x3444, 0x20 },
  102. { 0x3445, 0x25 },
  103. { 0x303a, 0x0c },
  104. { 0x3040, 0x00 },
  105. { 0x3041, 0x00 },
  106. { 0x303c, 0x00 },
  107. { 0x303d, 0x00 },
  108. { 0x3042, 0x9c },
  109. { 0x3043, 0x07 },
  110. { 0x303e, 0x49 },
  111. { 0x303f, 0x04 },
  112. { 0x304b, 0x0a },
  113. { 0x300f, 0x00 },
  114. { 0x3010, 0x21 },
  115. { 0x3012, 0x64 },
  116. { 0x3016, 0x09 },
  117. { 0x3070, 0x02 },
  118. { 0x3071, 0x11 },
  119. { 0x309b, 0x10 },
  120. { 0x309c, 0x22 },
  121. { 0x30a2, 0x02 },
  122. { 0x30a6, 0x20 },
  123. { 0x30a8, 0x20 },
  124. { 0x30aa, 0x20 },
  125. { 0x30ac, 0x20 },
  126. { 0x30b0, 0x43 },
  127. { 0x3119, 0x9e },
  128. { 0x311c, 0x1e },
  129. { 0x311e, 0x08 },
  130. { 0x3128, 0x05 },
  131. { 0x313d, 0x83 },
  132. { 0x3150, 0x03 },
  133. { 0x317e, 0x00 },
  134. { 0x32b8, 0x50 },
  135. { 0x32b9, 0x10 },
  136. { 0x32ba, 0x00 },
  137. { 0x32bb, 0x04 },
  138. { 0x32c8, 0x50 },
  139. { 0x32c9, 0x10 },
  140. { 0x32ca, 0x00 },
  141. { 0x32cb, 0x04 },
  142. { 0x332c, 0xd3 },
  143. { 0x332d, 0x10 },
  144. { 0x332e, 0x0d },
  145. { 0x3358, 0x06 },
  146. { 0x3359, 0xe1 },
  147. { 0x335a, 0x11 },
  148. { 0x3360, 0x1e },
  149. { 0x3361, 0x61 },
  150. { 0x3362, 0x10 },
  151. { 0x33b0, 0x50 },
  152. { 0x33b2, 0x1a },
  153. { 0x33b3, 0x04 },
  154. };
  155. static const struct imx290_regval imx290_1080p_settings[] = {
  156. /* mode settings */
  157. { 0x3007, 0x00 },
  158. { 0x303a, 0x0c },
  159. { 0x3414, 0x0a },
  160. { 0x3472, 0x80 },
  161. { 0x3473, 0x07 },
  162. { 0x3418, 0x38 },
  163. { 0x3419, 0x04 },
  164. { 0x3012, 0x64 },
  165. { 0x3013, 0x00 },
  166. { 0x305c, 0x18 },
  167. { 0x305d, 0x03 },
  168. { 0x305e, 0x20 },
  169. { 0x305f, 0x01 },
  170. { 0x315e, 0x1a },
  171. { 0x3164, 0x1a },
  172. { 0x3480, 0x49 },
  173. /* data rate settings */
  174. { 0x3405, 0x10 },
  175. { 0x3446, 0x57 },
  176. { 0x3447, 0x00 },
  177. { 0x3448, 0x37 },
  178. { 0x3449, 0x00 },
  179. { 0x344a, 0x1f },
  180. { 0x344b, 0x00 },
  181. { 0x344c, 0x1f },
  182. { 0x344d, 0x00 },
  183. { 0x344e, 0x1f },
  184. { 0x344f, 0x00 },
  185. { 0x3450, 0x77 },
  186. { 0x3451, 0x00 },
  187. { 0x3452, 0x1f },
  188. { 0x3453, 0x00 },
  189. { 0x3454, 0x17 },
  190. { 0x3455, 0x00 },
  191. };
  192. static const struct imx290_regval imx290_720p_settings[] = {
  193. /* mode settings */
  194. { 0x3007, 0x10 },
  195. { 0x303a, 0x06 },
  196. { 0x3414, 0x04 },
  197. { 0x3472, 0x00 },
  198. { 0x3473, 0x05 },
  199. { 0x3418, 0xd0 },
  200. { 0x3419, 0x02 },
  201. { 0x3012, 0x64 },
  202. { 0x3013, 0x00 },
  203. { 0x305c, 0x20 },
  204. { 0x305d, 0x00 },
  205. { 0x305e, 0x20 },
  206. { 0x305f, 0x01 },
  207. { 0x315e, 0x1a },
  208. { 0x3164, 0x1a },
  209. { 0x3480, 0x49 },
  210. /* data rate settings */
  211. { 0x3405, 0x10 },
  212. { 0x3446, 0x4f },
  213. { 0x3447, 0x00 },
  214. { 0x3448, 0x2f },
  215. { 0x3449, 0x00 },
  216. { 0x344a, 0x17 },
  217. { 0x344b, 0x00 },
  218. { 0x344c, 0x17 },
  219. { 0x344d, 0x00 },
  220. { 0x344e, 0x17 },
  221. { 0x344f, 0x00 },
  222. { 0x3450, 0x57 },
  223. { 0x3451, 0x00 },
  224. { 0x3452, 0x17 },
  225. { 0x3453, 0x00 },
  226. { 0x3454, 0x17 },
  227. { 0x3455, 0x00 },
  228. };
  229. static const struct imx290_regval imx290_10bit_settings[] = {
  230. { 0x3005, 0x00},
  231. { 0x3046, 0x00},
  232. { 0x3129, 0x1d},
  233. { 0x317c, 0x12},
  234. { 0x31ec, 0x37},
  235. { 0x3441, 0x0a},
  236. { 0x3442, 0x0a},
  237. { 0x300a, 0x3c},
  238. { 0x300b, 0x00},
  239. };
  240. static const struct imx290_regval imx290_12bit_settings[] = {
  241. { 0x3005, 0x01 },
  242. { 0x3046, 0x01 },
  243. { 0x3129, 0x00 },
  244. { 0x317c, 0x00 },
  245. { 0x31ec, 0x0e },
  246. { 0x3441, 0x0c },
  247. { 0x3442, 0x0c },
  248. { 0x300a, 0xf0 },
  249. { 0x300b, 0x00 },
  250. };
  251. /* supported link frequencies */
  252. #define FREQ_INDEX_1080P 0
  253. #define FREQ_INDEX_720P 1
  254. static const s64 imx290_link_freq_2lanes[] = {
  255. [FREQ_INDEX_1080P] = 445500000,
  256. [FREQ_INDEX_720P] = 297000000,
  257. };
  258. static const s64 imx290_link_freq_4lanes[] = {
  259. [FREQ_INDEX_1080P] = 222750000,
  260. [FREQ_INDEX_720P] = 148500000,
  261. };
  262. /*
  263. * In this function and in the similar ones below We rely on imx290_probe()
  264. * to ensure that nlanes is either 2 or 4.
  265. */
  266. static inline const s64 *imx290_link_freqs_ptr(const struct imx290 *imx290)
  267. {
  268. if (imx290->nlanes == 2)
  269. return imx290_link_freq_2lanes;
  270. else
  271. return imx290_link_freq_4lanes;
  272. }
  273. static inline int imx290_link_freqs_num(const struct imx290 *imx290)
  274. {
  275. if (imx290->nlanes == 2)
  276. return ARRAY_SIZE(imx290_link_freq_2lanes);
  277. else
  278. return ARRAY_SIZE(imx290_link_freq_4lanes);
  279. }
  280. /* Mode configs */
  281. static const struct imx290_mode imx290_modes_2lanes[] = {
  282. {
  283. .width = 1920,
  284. .height = 1080,
  285. .hmax = 0x1130,
  286. .link_freq_index = FREQ_INDEX_1080P,
  287. .data = imx290_1080p_settings,
  288. .data_size = ARRAY_SIZE(imx290_1080p_settings),
  289. },
  290. {
  291. .width = 1280,
  292. .height = 720,
  293. .hmax = 0x19c8,
  294. .link_freq_index = FREQ_INDEX_720P,
  295. .data = imx290_720p_settings,
  296. .data_size = ARRAY_SIZE(imx290_720p_settings),
  297. },
  298. };
  299. static const struct imx290_mode imx290_modes_4lanes[] = {
  300. {
  301. .width = 1920,
  302. .height = 1080,
  303. .hmax = 0x0898,
  304. .link_freq_index = FREQ_INDEX_1080P,
  305. .data = imx290_1080p_settings,
  306. .data_size = ARRAY_SIZE(imx290_1080p_settings),
  307. },
  308. {
  309. .width = 1280,
  310. .height = 720,
  311. .hmax = 0x0ce4,
  312. .link_freq_index = FREQ_INDEX_720P,
  313. .data = imx290_720p_settings,
  314. .data_size = ARRAY_SIZE(imx290_720p_settings),
  315. },
  316. };
  317. static inline const struct imx290_mode *imx290_modes_ptr(const struct imx290 *imx290)
  318. {
  319. if (imx290->nlanes == 2)
  320. return imx290_modes_2lanes;
  321. else
  322. return imx290_modes_4lanes;
  323. }
  324. static inline int imx290_modes_num(const struct imx290 *imx290)
  325. {
  326. if (imx290->nlanes == 2)
  327. return ARRAY_SIZE(imx290_modes_2lanes);
  328. else
  329. return ARRAY_SIZE(imx290_modes_4lanes);
  330. }
  331. static inline struct imx290 *to_imx290(struct v4l2_subdev *_sd)
  332. {
  333. return container_of(_sd, struct imx290, sd);
  334. }
  335. static inline int __always_unused imx290_read_reg(struct imx290 *imx290, u16 addr, u8 *value)
  336. {
  337. unsigned int regval;
  338. int ret;
  339. ret = regmap_read(imx290->regmap, addr, &regval);
  340. if (ret) {
  341. dev_err(imx290->dev, "I2C read failed for addr: %x\n", addr);
  342. return ret;
  343. }
  344. *value = regval & 0xff;
  345. return 0;
  346. }
  347. static int imx290_write_reg(struct imx290 *imx290, u16 addr, u8 value)
  348. {
  349. int ret;
  350. ret = regmap_write(imx290->regmap, addr, value);
  351. if (ret) {
  352. dev_err(imx290->dev, "I2C write failed for addr: %x\n", addr);
  353. return ret;
  354. }
  355. return ret;
  356. }
  357. static int imx290_set_register_array(struct imx290 *imx290,
  358. const struct imx290_regval *settings,
  359. unsigned int num_settings)
  360. {
  361. unsigned int i;
  362. int ret;
  363. for (i = 0; i < num_settings; ++i, ++settings) {
  364. ret = imx290_write_reg(imx290, settings->reg, settings->val);
  365. if (ret < 0)
  366. return ret;
  367. }
  368. /* Provide 10ms settle time */
  369. usleep_range(10000, 11000);
  370. return 0;
  371. }
  372. static int imx290_write_buffered_reg(struct imx290 *imx290, u16 address_low,
  373. u8 nr_regs, u32 value)
  374. {
  375. unsigned int i;
  376. int ret;
  377. ret = imx290_write_reg(imx290, IMX290_REGHOLD, 0x01);
  378. if (ret) {
  379. dev_err(imx290->dev, "Error setting hold register\n");
  380. return ret;
  381. }
  382. for (i = 0; i < nr_regs; i++) {
  383. ret = imx290_write_reg(imx290, address_low + i,
  384. (u8)(value >> (i * 8)));
  385. if (ret) {
  386. dev_err(imx290->dev, "Error writing buffered registers\n");
  387. return ret;
  388. }
  389. }
  390. ret = imx290_write_reg(imx290, IMX290_REGHOLD, 0x00);
  391. if (ret) {
  392. dev_err(imx290->dev, "Error setting hold register\n");
  393. return ret;
  394. }
  395. return ret;
  396. }
  397. static int imx290_set_gain(struct imx290 *imx290, u32 value)
  398. {
  399. int ret;
  400. ret = imx290_write_buffered_reg(imx290, IMX290_GAIN, 1, value);
  401. if (ret)
  402. dev_err(imx290->dev, "Unable to write gain\n");
  403. return ret;
  404. }
  405. /* Stop streaming */
  406. static int imx290_stop_streaming(struct imx290 *imx290)
  407. {
  408. int ret;
  409. ret = imx290_write_reg(imx290, IMX290_STANDBY, 0x01);
  410. if (ret < 0)
  411. return ret;
  412. msleep(30);
  413. return imx290_write_reg(imx290, IMX290_XMSTA, 0x01);
  414. }
  415. static int imx290_set_ctrl(struct v4l2_ctrl *ctrl)
  416. {
  417. struct imx290 *imx290 = container_of(ctrl->handler,
  418. struct imx290, ctrls);
  419. int ret = 0;
  420. /* V4L2 controls values will be applied only when power is already up */
  421. if (!pm_runtime_get_if_in_use(imx290->dev))
  422. return 0;
  423. switch (ctrl->id) {
  424. case V4L2_CID_GAIN:
  425. ret = imx290_set_gain(imx290, ctrl->val);
  426. break;
  427. case V4L2_CID_TEST_PATTERN:
  428. if (ctrl->val) {
  429. imx290_write_reg(imx290, IMX290_BLKLEVEL_LOW, 0x00);
  430. imx290_write_reg(imx290, IMX290_BLKLEVEL_HIGH, 0x00);
  431. usleep_range(10000, 11000);
  432. imx290_write_reg(imx290, IMX290_PGCTRL,
  433. (u8)(IMX290_PGCTRL_REGEN |
  434. IMX290_PGCTRL_THRU |
  435. IMX290_PGCTRL_MODE(ctrl->val)));
  436. } else {
  437. imx290_write_reg(imx290, IMX290_PGCTRL, 0x00);
  438. usleep_range(10000, 11000);
  439. if (imx290->bpp == 10)
  440. imx290_write_reg(imx290, IMX290_BLKLEVEL_LOW,
  441. 0x3c);
  442. else /* 12 bits per pixel */
  443. imx290_write_reg(imx290, IMX290_BLKLEVEL_LOW,
  444. 0xf0);
  445. imx290_write_reg(imx290, IMX290_BLKLEVEL_HIGH, 0x00);
  446. }
  447. break;
  448. default:
  449. ret = -EINVAL;
  450. break;
  451. }
  452. pm_runtime_put(imx290->dev);
  453. return ret;
  454. }
  455. static const struct v4l2_ctrl_ops imx290_ctrl_ops = {
  456. .s_ctrl = imx290_set_ctrl,
  457. };
  458. static int imx290_enum_mbus_code(struct v4l2_subdev *sd,
  459. struct v4l2_subdev_state *sd_state,
  460. struct v4l2_subdev_mbus_code_enum *code)
  461. {
  462. if (code->index >= ARRAY_SIZE(imx290_formats))
  463. return -EINVAL;
  464. code->code = imx290_formats[code->index].code;
  465. return 0;
  466. }
  467. static int imx290_enum_frame_size(struct v4l2_subdev *sd,
  468. struct v4l2_subdev_state *sd_state,
  469. struct v4l2_subdev_frame_size_enum *fse)
  470. {
  471. const struct imx290 *imx290 = to_imx290(sd);
  472. const struct imx290_mode *imx290_modes = imx290_modes_ptr(imx290);
  473. if ((fse->code != imx290_formats[0].code) &&
  474. (fse->code != imx290_formats[1].code))
  475. return -EINVAL;
  476. if (fse->index >= imx290_modes_num(imx290))
  477. return -EINVAL;
  478. fse->min_width = imx290_modes[fse->index].width;
  479. fse->max_width = imx290_modes[fse->index].width;
  480. fse->min_height = imx290_modes[fse->index].height;
  481. fse->max_height = imx290_modes[fse->index].height;
  482. return 0;
  483. }
  484. static int imx290_get_fmt(struct v4l2_subdev *sd,
  485. struct v4l2_subdev_state *sd_state,
  486. struct v4l2_subdev_format *fmt)
  487. {
  488. struct imx290 *imx290 = to_imx290(sd);
  489. struct v4l2_mbus_framefmt *framefmt;
  490. mutex_lock(&imx290->lock);
  491. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
  492. framefmt = v4l2_subdev_get_try_format(&imx290->sd, sd_state,
  493. fmt->pad);
  494. else
  495. framefmt = &imx290->current_format;
  496. fmt->format = *framefmt;
  497. mutex_unlock(&imx290->lock);
  498. return 0;
  499. }
  500. static inline u8 imx290_get_link_freq_index(struct imx290 *imx290)
  501. {
  502. return imx290->current_mode->link_freq_index;
  503. }
  504. static s64 imx290_get_link_freq(struct imx290 *imx290)
  505. {
  506. u8 index = imx290_get_link_freq_index(imx290);
  507. return *(imx290_link_freqs_ptr(imx290) + index);
  508. }
  509. static u64 imx290_calc_pixel_rate(struct imx290 *imx290)
  510. {
  511. s64 link_freq = imx290_get_link_freq(imx290);
  512. u8 nlanes = imx290->nlanes;
  513. u64 pixel_rate;
  514. /* pixel rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
  515. pixel_rate = link_freq * 2 * nlanes;
  516. do_div(pixel_rate, imx290->bpp);
  517. return pixel_rate;
  518. }
  519. static int imx290_set_fmt(struct v4l2_subdev *sd,
  520. struct v4l2_subdev_state *sd_state,
  521. struct v4l2_subdev_format *fmt)
  522. {
  523. struct imx290 *imx290 = to_imx290(sd);
  524. const struct imx290_mode *mode;
  525. struct v4l2_mbus_framefmt *format;
  526. unsigned int i;
  527. mutex_lock(&imx290->lock);
  528. mode = v4l2_find_nearest_size(imx290_modes_ptr(imx290),
  529. imx290_modes_num(imx290), width, height,
  530. fmt->format.width, fmt->format.height);
  531. fmt->format.width = mode->width;
  532. fmt->format.height = mode->height;
  533. for (i = 0; i < ARRAY_SIZE(imx290_formats); i++)
  534. if (imx290_formats[i].code == fmt->format.code)
  535. break;
  536. if (i >= ARRAY_SIZE(imx290_formats))
  537. i = 0;
  538. fmt->format.code = imx290_formats[i].code;
  539. fmt->format.field = V4L2_FIELD_NONE;
  540. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  541. format = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
  542. } else {
  543. format = &imx290->current_format;
  544. imx290->current_mode = mode;
  545. imx290->bpp = imx290_formats[i].bpp;
  546. if (imx290->link_freq)
  547. __v4l2_ctrl_s_ctrl(imx290->link_freq,
  548. imx290_get_link_freq_index(imx290));
  549. if (imx290->pixel_rate)
  550. __v4l2_ctrl_s_ctrl_int64(imx290->pixel_rate,
  551. imx290_calc_pixel_rate(imx290));
  552. }
  553. *format = fmt->format;
  554. mutex_unlock(&imx290->lock);
  555. return 0;
  556. }
  557. static int imx290_entity_init_cfg(struct v4l2_subdev *subdev,
  558. struct v4l2_subdev_state *sd_state)
  559. {
  560. struct v4l2_subdev_format fmt = { 0 };
  561. fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  562. fmt.format.width = 1920;
  563. fmt.format.height = 1080;
  564. imx290_set_fmt(subdev, sd_state, &fmt);
  565. return 0;
  566. }
  567. static int imx290_write_current_format(struct imx290 *imx290)
  568. {
  569. int ret;
  570. switch (imx290->current_format.code) {
  571. case MEDIA_BUS_FMT_SRGGB10_1X10:
  572. ret = imx290_set_register_array(imx290, imx290_10bit_settings,
  573. ARRAY_SIZE(
  574. imx290_10bit_settings));
  575. if (ret < 0) {
  576. dev_err(imx290->dev, "Could not set format registers\n");
  577. return ret;
  578. }
  579. break;
  580. case MEDIA_BUS_FMT_SRGGB12_1X12:
  581. ret = imx290_set_register_array(imx290, imx290_12bit_settings,
  582. ARRAY_SIZE(
  583. imx290_12bit_settings));
  584. if (ret < 0) {
  585. dev_err(imx290->dev, "Could not set format registers\n");
  586. return ret;
  587. }
  588. break;
  589. default:
  590. dev_err(imx290->dev, "Unknown pixel format\n");
  591. return -EINVAL;
  592. }
  593. return 0;
  594. }
  595. static int imx290_set_hmax(struct imx290 *imx290, u32 val)
  596. {
  597. int ret;
  598. ret = imx290_write_reg(imx290, IMX290_HMAX_LOW, (val & 0xff));
  599. if (ret) {
  600. dev_err(imx290->dev, "Error setting HMAX register\n");
  601. return ret;
  602. }
  603. ret = imx290_write_reg(imx290, IMX290_HMAX_HIGH, ((val >> 8) & 0xff));
  604. if (ret) {
  605. dev_err(imx290->dev, "Error setting HMAX register\n");
  606. return ret;
  607. }
  608. return 0;
  609. }
  610. /* Start streaming */
  611. static int imx290_start_streaming(struct imx290 *imx290)
  612. {
  613. int ret;
  614. /* Set init register settings */
  615. ret = imx290_set_register_array(imx290, imx290_global_init_settings,
  616. ARRAY_SIZE(
  617. imx290_global_init_settings));
  618. if (ret < 0) {
  619. dev_err(imx290->dev, "Could not set init registers\n");
  620. return ret;
  621. }
  622. /* Apply the register values related to current frame format */
  623. ret = imx290_write_current_format(imx290);
  624. if (ret < 0) {
  625. dev_err(imx290->dev, "Could not set frame format\n");
  626. return ret;
  627. }
  628. /* Apply default values of current mode */
  629. ret = imx290_set_register_array(imx290, imx290->current_mode->data,
  630. imx290->current_mode->data_size);
  631. if (ret < 0) {
  632. dev_err(imx290->dev, "Could not set current mode\n");
  633. return ret;
  634. }
  635. ret = imx290_set_hmax(imx290, imx290->current_mode->hmax);
  636. if (ret < 0)
  637. return ret;
  638. /* Apply customized values from user */
  639. ret = v4l2_ctrl_handler_setup(imx290->sd.ctrl_handler);
  640. if (ret) {
  641. dev_err(imx290->dev, "Could not sync v4l2 controls\n");
  642. return ret;
  643. }
  644. ret = imx290_write_reg(imx290, IMX290_STANDBY, 0x00);
  645. if (ret < 0)
  646. return ret;
  647. msleep(30);
  648. /* Start streaming */
  649. return imx290_write_reg(imx290, IMX290_XMSTA, 0x00);
  650. }
  651. static int imx290_set_stream(struct v4l2_subdev *sd, int enable)
  652. {
  653. struct imx290 *imx290 = to_imx290(sd);
  654. int ret = 0;
  655. if (enable) {
  656. ret = pm_runtime_resume_and_get(imx290->dev);
  657. if (ret < 0)
  658. goto unlock_and_return;
  659. ret = imx290_start_streaming(imx290);
  660. if (ret) {
  661. dev_err(imx290->dev, "Start stream failed\n");
  662. pm_runtime_put(imx290->dev);
  663. goto unlock_and_return;
  664. }
  665. } else {
  666. imx290_stop_streaming(imx290);
  667. pm_runtime_put(imx290->dev);
  668. }
  669. unlock_and_return:
  670. return ret;
  671. }
  672. static int imx290_get_regulators(struct device *dev, struct imx290 *imx290)
  673. {
  674. unsigned int i;
  675. for (i = 0; i < IMX290_NUM_SUPPLIES; i++)
  676. imx290->supplies[i].supply = imx290_supply_name[i];
  677. return devm_regulator_bulk_get(dev, IMX290_NUM_SUPPLIES,
  678. imx290->supplies);
  679. }
  680. static int imx290_set_data_lanes(struct imx290 *imx290)
  681. {
  682. int ret = 0, laneval, frsel;
  683. switch (imx290->nlanes) {
  684. case 2:
  685. laneval = 0x01;
  686. frsel = 0x02;
  687. break;
  688. case 4:
  689. laneval = 0x03;
  690. frsel = 0x01;
  691. break;
  692. default:
  693. /*
  694. * We should never hit this since the data lane count is
  695. * validated in probe itself
  696. */
  697. dev_err(imx290->dev, "Lane configuration not supported\n");
  698. ret = -EINVAL;
  699. goto exit;
  700. }
  701. ret = imx290_write_reg(imx290, IMX290_PHY_LANE_NUM, laneval);
  702. if (ret) {
  703. dev_err(imx290->dev, "Error setting Physical Lane number register\n");
  704. goto exit;
  705. }
  706. ret = imx290_write_reg(imx290, IMX290_CSI_LANE_MODE, laneval);
  707. if (ret) {
  708. dev_err(imx290->dev, "Error setting CSI Lane mode register\n");
  709. goto exit;
  710. }
  711. ret = imx290_write_reg(imx290, IMX290_FR_FDG_SEL, frsel);
  712. if (ret)
  713. dev_err(imx290->dev, "Error setting FR/FDG SEL register\n");
  714. exit:
  715. return ret;
  716. }
  717. static int imx290_power_on(struct device *dev)
  718. {
  719. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  720. struct imx290 *imx290 = to_imx290(sd);
  721. int ret;
  722. ret = clk_prepare_enable(imx290->xclk);
  723. if (ret) {
  724. dev_err(dev, "Failed to enable clock\n");
  725. return ret;
  726. }
  727. ret = regulator_bulk_enable(IMX290_NUM_SUPPLIES, imx290->supplies);
  728. if (ret) {
  729. dev_err(dev, "Failed to enable regulators\n");
  730. clk_disable_unprepare(imx290->xclk);
  731. return ret;
  732. }
  733. usleep_range(1, 2);
  734. gpiod_set_value_cansleep(imx290->rst_gpio, 0);
  735. usleep_range(30000, 31000);
  736. /* Set data lane count */
  737. imx290_set_data_lanes(imx290);
  738. return 0;
  739. }
  740. static int imx290_power_off(struct device *dev)
  741. {
  742. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  743. struct imx290 *imx290 = to_imx290(sd);
  744. clk_disable_unprepare(imx290->xclk);
  745. gpiod_set_value_cansleep(imx290->rst_gpio, 1);
  746. regulator_bulk_disable(IMX290_NUM_SUPPLIES, imx290->supplies);
  747. return 0;
  748. }
  749. static const struct dev_pm_ops imx290_pm_ops = {
  750. SET_RUNTIME_PM_OPS(imx290_power_off, imx290_power_on, NULL)
  751. };
  752. static const struct v4l2_subdev_video_ops imx290_video_ops = {
  753. .s_stream = imx290_set_stream,
  754. };
  755. static const struct v4l2_subdev_pad_ops imx290_pad_ops = {
  756. .init_cfg = imx290_entity_init_cfg,
  757. .enum_mbus_code = imx290_enum_mbus_code,
  758. .enum_frame_size = imx290_enum_frame_size,
  759. .get_fmt = imx290_get_fmt,
  760. .set_fmt = imx290_set_fmt,
  761. };
  762. static const struct v4l2_subdev_ops imx290_subdev_ops = {
  763. .video = &imx290_video_ops,
  764. .pad = &imx290_pad_ops,
  765. };
  766. static const struct media_entity_operations imx290_subdev_entity_ops = {
  767. .link_validate = v4l2_subdev_link_validate,
  768. };
  769. /*
  770. * Returns 0 if all link frequencies used by the driver for the given number
  771. * of MIPI data lanes are mentioned in the device tree, or the value of the
  772. * first missing frequency otherwise.
  773. */
  774. static s64 imx290_check_link_freqs(const struct imx290 *imx290,
  775. const struct v4l2_fwnode_endpoint *ep)
  776. {
  777. int i, j;
  778. const s64 *freqs = imx290_link_freqs_ptr(imx290);
  779. int freqs_count = imx290_link_freqs_num(imx290);
  780. for (i = 0; i < freqs_count; i++) {
  781. for (j = 0; j < ep->nr_of_link_frequencies; j++)
  782. if (freqs[i] == ep->link_frequencies[j])
  783. break;
  784. if (j == ep->nr_of_link_frequencies)
  785. return freqs[i];
  786. }
  787. return 0;
  788. }
  789. static int imx290_probe(struct i2c_client *client)
  790. {
  791. struct device *dev = &client->dev;
  792. struct fwnode_handle *endpoint;
  793. /* Only CSI2 is supported for now: */
  794. struct v4l2_fwnode_endpoint ep = {
  795. .bus_type = V4L2_MBUS_CSI2_DPHY
  796. };
  797. struct imx290 *imx290;
  798. u32 xclk_freq;
  799. s64 fq;
  800. int ret;
  801. imx290 = devm_kzalloc(dev, sizeof(*imx290), GFP_KERNEL);
  802. if (!imx290)
  803. return -ENOMEM;
  804. imx290->dev = dev;
  805. imx290->regmap = devm_regmap_init_i2c(client, &imx290_regmap_config);
  806. if (IS_ERR(imx290->regmap)) {
  807. dev_err(dev, "Unable to initialize I2C\n");
  808. return -ENODEV;
  809. }
  810. endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
  811. if (!endpoint) {
  812. dev_err(dev, "Endpoint node not found\n");
  813. return -EINVAL;
  814. }
  815. ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep);
  816. fwnode_handle_put(endpoint);
  817. if (ret == -ENXIO) {
  818. dev_err(dev, "Unsupported bus type, should be CSI2\n");
  819. goto free_err;
  820. } else if (ret) {
  821. dev_err(dev, "Parsing endpoint node failed\n");
  822. goto free_err;
  823. }
  824. /* Get number of data lanes */
  825. imx290->nlanes = ep.bus.mipi_csi2.num_data_lanes;
  826. if (imx290->nlanes != 2 && imx290->nlanes != 4) {
  827. dev_err(dev, "Invalid data lanes: %d\n", imx290->nlanes);
  828. ret = -EINVAL;
  829. goto free_err;
  830. }
  831. dev_dbg(dev, "Using %u data lanes\n", imx290->nlanes);
  832. if (!ep.nr_of_link_frequencies) {
  833. dev_err(dev, "link-frequency property not found in DT\n");
  834. ret = -EINVAL;
  835. goto free_err;
  836. }
  837. /* Check that link frequences for all the modes are in device tree */
  838. fq = imx290_check_link_freqs(imx290, &ep);
  839. if (fq) {
  840. dev_err(dev, "Link frequency of %lld is not supported\n", fq);
  841. ret = -EINVAL;
  842. goto free_err;
  843. }
  844. /* get system clock (xclk) */
  845. imx290->xclk = devm_clk_get(dev, "xclk");
  846. if (IS_ERR(imx290->xclk)) {
  847. dev_err(dev, "Could not get xclk");
  848. ret = PTR_ERR(imx290->xclk);
  849. goto free_err;
  850. }
  851. ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
  852. &xclk_freq);
  853. if (ret) {
  854. dev_err(dev, "Could not get xclk frequency\n");
  855. goto free_err;
  856. }
  857. /* external clock must be 37.125 MHz */
  858. if (xclk_freq != 37125000) {
  859. dev_err(dev, "External clock frequency %u is not supported\n",
  860. xclk_freq);
  861. ret = -EINVAL;
  862. goto free_err;
  863. }
  864. ret = clk_set_rate(imx290->xclk, xclk_freq);
  865. if (ret) {
  866. dev_err(dev, "Could not set xclk frequency\n");
  867. goto free_err;
  868. }
  869. ret = imx290_get_regulators(dev, imx290);
  870. if (ret < 0) {
  871. dev_err(dev, "Cannot get regulators\n");
  872. goto free_err;
  873. }
  874. imx290->rst_gpio = devm_gpiod_get_optional(dev, "reset",
  875. GPIOD_OUT_HIGH);
  876. if (IS_ERR(imx290->rst_gpio)) {
  877. dev_err(dev, "Cannot get reset gpio\n");
  878. ret = PTR_ERR(imx290->rst_gpio);
  879. goto free_err;
  880. }
  881. mutex_init(&imx290->lock);
  882. /*
  883. * Initialize the frame format. In particular, imx290->current_mode
  884. * and imx290->bpp are set to defaults: imx290_calc_pixel_rate() call
  885. * below relies on these fields.
  886. */
  887. imx290_entity_init_cfg(&imx290->sd, NULL);
  888. v4l2_ctrl_handler_init(&imx290->ctrls, 4);
  889. v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
  890. V4L2_CID_GAIN, 0, 72, 1, 0);
  891. imx290->link_freq =
  892. v4l2_ctrl_new_int_menu(&imx290->ctrls, &imx290_ctrl_ops,
  893. V4L2_CID_LINK_FREQ,
  894. imx290_link_freqs_num(imx290) - 1, 0,
  895. imx290_link_freqs_ptr(imx290));
  896. if (imx290->link_freq)
  897. imx290->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  898. imx290->pixel_rate = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
  899. V4L2_CID_PIXEL_RATE,
  900. 1, INT_MAX, 1,
  901. imx290_calc_pixel_rate(imx290));
  902. v4l2_ctrl_new_std_menu_items(&imx290->ctrls, &imx290_ctrl_ops,
  903. V4L2_CID_TEST_PATTERN,
  904. ARRAY_SIZE(imx290_test_pattern_menu) - 1,
  905. 0, 0, imx290_test_pattern_menu);
  906. imx290->sd.ctrl_handler = &imx290->ctrls;
  907. if (imx290->ctrls.error) {
  908. dev_err(dev, "Control initialization error %d\n",
  909. imx290->ctrls.error);
  910. ret = imx290->ctrls.error;
  911. goto free_ctrl;
  912. }
  913. v4l2_i2c_subdev_init(&imx290->sd, client, &imx290_subdev_ops);
  914. imx290->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  915. imx290->sd.dev = &client->dev;
  916. imx290->sd.entity.ops = &imx290_subdev_entity_ops;
  917. imx290->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  918. imx290->pad.flags = MEDIA_PAD_FL_SOURCE;
  919. ret = media_entity_pads_init(&imx290->sd.entity, 1, &imx290->pad);
  920. if (ret < 0) {
  921. dev_err(dev, "Could not register media entity\n");
  922. goto free_ctrl;
  923. }
  924. ret = v4l2_async_register_subdev(&imx290->sd);
  925. if (ret < 0) {
  926. dev_err(dev, "Could not register v4l2 device\n");
  927. goto free_entity;
  928. }
  929. /* Power on the device to match runtime PM state below */
  930. ret = imx290_power_on(dev);
  931. if (ret < 0) {
  932. dev_err(dev, "Could not power on the device\n");
  933. goto free_entity;
  934. }
  935. pm_runtime_set_active(dev);
  936. pm_runtime_enable(dev);
  937. pm_runtime_idle(dev);
  938. v4l2_fwnode_endpoint_free(&ep);
  939. return 0;
  940. free_entity:
  941. media_entity_cleanup(&imx290->sd.entity);
  942. free_ctrl:
  943. v4l2_ctrl_handler_free(&imx290->ctrls);
  944. mutex_destroy(&imx290->lock);
  945. free_err:
  946. v4l2_fwnode_endpoint_free(&ep);
  947. return ret;
  948. }
  949. static void imx290_remove(struct i2c_client *client)
  950. {
  951. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  952. struct imx290 *imx290 = to_imx290(sd);
  953. v4l2_async_unregister_subdev(sd);
  954. media_entity_cleanup(&sd->entity);
  955. v4l2_ctrl_handler_free(sd->ctrl_handler);
  956. mutex_destroy(&imx290->lock);
  957. pm_runtime_disable(imx290->dev);
  958. if (!pm_runtime_status_suspended(imx290->dev))
  959. imx290_power_off(imx290->dev);
  960. pm_runtime_set_suspended(imx290->dev);
  961. }
  962. static const struct of_device_id imx290_of_match[] = {
  963. { .compatible = "sony,imx290" },
  964. { /* sentinel */ }
  965. };
  966. MODULE_DEVICE_TABLE(of, imx290_of_match);
  967. static struct i2c_driver imx290_i2c_driver = {
  968. .probe_new = imx290_probe,
  969. .remove = imx290_remove,
  970. .driver = {
  971. .name = "imx290",
  972. .pm = &imx290_pm_ops,
  973. .of_match_table = of_match_ptr(imx290_of_match),
  974. },
  975. };
  976. module_i2c_driver(imx290_i2c_driver);
  977. MODULE_DESCRIPTION("Sony IMX290 CMOS Image Sensor Driver");
  978. MODULE_AUTHOR("FRAMOS GmbH");
  979. MODULE_AUTHOR("Manivannan Sadhasivam <[email protected]>");
  980. MODULE_LICENSE("GPL v2");