imx214.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * imx214.c - imx214 sensor driver
  4. *
  5. * Copyright 2018 Qtechnology A/S
  6. *
  7. * Ricardo Ribalda <[email protected]>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/i2c.h>
  13. #include <linux/module.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/regmap.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <media/media-entity.h>
  18. #include <media/v4l2-ctrls.h>
  19. #include <media/v4l2-fwnode.h>
  20. #include <media/v4l2-subdev.h>
  21. #define IMX214_DEFAULT_CLK_FREQ 24000000
  22. #define IMX214_DEFAULT_LINK_FREQ 480000000
  23. #define IMX214_DEFAULT_PIXEL_RATE ((IMX214_DEFAULT_LINK_FREQ * 8LL) / 10)
  24. #define IMX214_FPS 30
  25. #define IMX214_MBUS_CODE MEDIA_BUS_FMT_SRGGB10_1X10
  26. static const char * const imx214_supply_name[] = {
  27. "vdda",
  28. "vddd",
  29. "vdddo",
  30. };
  31. #define IMX214_NUM_SUPPLIES ARRAY_SIZE(imx214_supply_name)
  32. struct imx214 {
  33. struct device *dev;
  34. struct clk *xclk;
  35. struct regmap *regmap;
  36. struct v4l2_subdev sd;
  37. struct media_pad pad;
  38. struct v4l2_mbus_framefmt fmt;
  39. struct v4l2_rect crop;
  40. struct v4l2_ctrl_handler ctrls;
  41. struct v4l2_ctrl *pixel_rate;
  42. struct v4l2_ctrl *link_freq;
  43. struct v4l2_ctrl *exposure;
  44. struct v4l2_ctrl *unit_size;
  45. struct regulator_bulk_data supplies[IMX214_NUM_SUPPLIES];
  46. struct gpio_desc *enable_gpio;
  47. /*
  48. * Serialize control access, get/set format, get selection
  49. * and start streaming.
  50. */
  51. struct mutex mutex;
  52. bool streaming;
  53. };
  54. struct reg_8 {
  55. u16 addr;
  56. u8 val;
  57. };
  58. enum {
  59. IMX214_TABLE_WAIT_MS = 0,
  60. IMX214_TABLE_END,
  61. IMX214_MAX_RETRIES,
  62. IMX214_WAIT_MS
  63. };
  64. /*From imx214_mode_tbls.h*/
  65. static const struct reg_8 mode_4096x2304[] = {
  66. {0x0114, 0x03},
  67. {0x0220, 0x00},
  68. {0x0221, 0x11},
  69. {0x0222, 0x01},
  70. {0x0340, 0x0C},
  71. {0x0341, 0x7A},
  72. {0x0342, 0x13},
  73. {0x0343, 0x90},
  74. {0x0344, 0x00},
  75. {0x0345, 0x38},
  76. {0x0346, 0x01},
  77. {0x0347, 0x98},
  78. {0x0348, 0x10},
  79. {0x0349, 0x37},
  80. {0x034A, 0x0A},
  81. {0x034B, 0x97},
  82. {0x0381, 0x01},
  83. {0x0383, 0x01},
  84. {0x0385, 0x01},
  85. {0x0387, 0x01},
  86. {0x0900, 0x00},
  87. {0x0901, 0x00},
  88. {0x0902, 0x00},
  89. {0x3000, 0x35},
  90. {0x3054, 0x01},
  91. {0x305C, 0x11},
  92. {0x0112, 0x0A},
  93. {0x0113, 0x0A},
  94. {0x034C, 0x10},
  95. {0x034D, 0x00},
  96. {0x034E, 0x09},
  97. {0x034F, 0x00},
  98. {0x0401, 0x00},
  99. {0x0404, 0x00},
  100. {0x0405, 0x10},
  101. {0x0408, 0x00},
  102. {0x0409, 0x00},
  103. {0x040A, 0x00},
  104. {0x040B, 0x00},
  105. {0x040C, 0x10},
  106. {0x040D, 0x00},
  107. {0x040E, 0x09},
  108. {0x040F, 0x00},
  109. {0x0301, 0x05},
  110. {0x0303, 0x02},
  111. {0x0305, 0x03},
  112. {0x0306, 0x00},
  113. {0x0307, 0x96},
  114. {0x0309, 0x0A},
  115. {0x030B, 0x01},
  116. {0x0310, 0x00},
  117. {0x0820, 0x12},
  118. {0x0821, 0xC0},
  119. {0x0822, 0x00},
  120. {0x0823, 0x00},
  121. {0x3A03, 0x09},
  122. {0x3A04, 0x50},
  123. {0x3A05, 0x01},
  124. {0x0B06, 0x01},
  125. {0x30A2, 0x00},
  126. {0x30B4, 0x00},
  127. {0x3A02, 0xFF},
  128. {0x3011, 0x00},
  129. {0x3013, 0x01},
  130. {0x0202, 0x0C},
  131. {0x0203, 0x70},
  132. {0x0224, 0x01},
  133. {0x0225, 0xF4},
  134. {0x0204, 0x00},
  135. {0x0205, 0x00},
  136. {0x020E, 0x01},
  137. {0x020F, 0x00},
  138. {0x0210, 0x01},
  139. {0x0211, 0x00},
  140. {0x0212, 0x01},
  141. {0x0213, 0x00},
  142. {0x0214, 0x01},
  143. {0x0215, 0x00},
  144. {0x0216, 0x00},
  145. {0x0217, 0x00},
  146. {0x4170, 0x00},
  147. {0x4171, 0x10},
  148. {0x4176, 0x00},
  149. {0x4177, 0x3C},
  150. {0xAE20, 0x04},
  151. {0xAE21, 0x5C},
  152. {IMX214_TABLE_WAIT_MS, 10},
  153. {0x0138, 0x01},
  154. {IMX214_TABLE_END, 0x00}
  155. };
  156. static const struct reg_8 mode_1920x1080[] = {
  157. {0x0114, 0x03},
  158. {0x0220, 0x00},
  159. {0x0221, 0x11},
  160. {0x0222, 0x01},
  161. {0x0340, 0x0C},
  162. {0x0341, 0x7A},
  163. {0x0342, 0x13},
  164. {0x0343, 0x90},
  165. {0x0344, 0x04},
  166. {0x0345, 0x78},
  167. {0x0346, 0x03},
  168. {0x0347, 0xFC},
  169. {0x0348, 0x0B},
  170. {0x0349, 0xF7},
  171. {0x034A, 0x08},
  172. {0x034B, 0x33},
  173. {0x0381, 0x01},
  174. {0x0383, 0x01},
  175. {0x0385, 0x01},
  176. {0x0387, 0x01},
  177. {0x0900, 0x00},
  178. {0x0901, 0x00},
  179. {0x0902, 0x00},
  180. {0x3000, 0x35},
  181. {0x3054, 0x01},
  182. {0x305C, 0x11},
  183. {0x0112, 0x0A},
  184. {0x0113, 0x0A},
  185. {0x034C, 0x07},
  186. {0x034D, 0x80},
  187. {0x034E, 0x04},
  188. {0x034F, 0x38},
  189. {0x0401, 0x00},
  190. {0x0404, 0x00},
  191. {0x0405, 0x10},
  192. {0x0408, 0x00},
  193. {0x0409, 0x00},
  194. {0x040A, 0x00},
  195. {0x040B, 0x00},
  196. {0x040C, 0x07},
  197. {0x040D, 0x80},
  198. {0x040E, 0x04},
  199. {0x040F, 0x38},
  200. {0x0301, 0x05},
  201. {0x0303, 0x02},
  202. {0x0305, 0x03},
  203. {0x0306, 0x00},
  204. {0x0307, 0x96},
  205. {0x0309, 0x0A},
  206. {0x030B, 0x01},
  207. {0x0310, 0x00},
  208. {0x0820, 0x12},
  209. {0x0821, 0xC0},
  210. {0x0822, 0x00},
  211. {0x0823, 0x00},
  212. {0x3A03, 0x04},
  213. {0x3A04, 0xF8},
  214. {0x3A05, 0x02},
  215. {0x0B06, 0x01},
  216. {0x30A2, 0x00},
  217. {0x30B4, 0x00},
  218. {0x3A02, 0xFF},
  219. {0x3011, 0x00},
  220. {0x3013, 0x01},
  221. {0x0202, 0x0C},
  222. {0x0203, 0x70},
  223. {0x0224, 0x01},
  224. {0x0225, 0xF4},
  225. {0x0204, 0x00},
  226. {0x0205, 0x00},
  227. {0x020E, 0x01},
  228. {0x020F, 0x00},
  229. {0x0210, 0x01},
  230. {0x0211, 0x00},
  231. {0x0212, 0x01},
  232. {0x0213, 0x00},
  233. {0x0214, 0x01},
  234. {0x0215, 0x00},
  235. {0x0216, 0x00},
  236. {0x0217, 0x00},
  237. {0x4170, 0x00},
  238. {0x4171, 0x10},
  239. {0x4176, 0x00},
  240. {0x4177, 0x3C},
  241. {0xAE20, 0x04},
  242. {0xAE21, 0x5C},
  243. {IMX214_TABLE_WAIT_MS, 10},
  244. {0x0138, 0x01},
  245. {IMX214_TABLE_END, 0x00}
  246. };
  247. static const struct reg_8 mode_table_common[] = {
  248. /* software reset */
  249. /* software standby settings */
  250. {0x0100, 0x00},
  251. /* ATR setting */
  252. {0x9300, 0x02},
  253. /* external clock setting */
  254. {0x0136, 0x18},
  255. {0x0137, 0x00},
  256. /* global setting */
  257. /* basic config */
  258. {0x0101, 0x00},
  259. {0x0105, 0x01},
  260. {0x0106, 0x01},
  261. {0x4550, 0x02},
  262. {0x4601, 0x00},
  263. {0x4642, 0x05},
  264. {0x6227, 0x11},
  265. {0x6276, 0x00},
  266. {0x900E, 0x06},
  267. {0xA802, 0x90},
  268. {0xA803, 0x11},
  269. {0xA804, 0x62},
  270. {0xA805, 0x77},
  271. {0xA806, 0xAE},
  272. {0xA807, 0x34},
  273. {0xA808, 0xAE},
  274. {0xA809, 0x35},
  275. {0xA80A, 0x62},
  276. {0xA80B, 0x83},
  277. {0xAE33, 0x00},
  278. /* analog setting */
  279. {0x4174, 0x00},
  280. {0x4175, 0x11},
  281. {0x4612, 0x29},
  282. {0x461B, 0x12},
  283. {0x461F, 0x06},
  284. {0x4635, 0x07},
  285. {0x4637, 0x30},
  286. {0x463F, 0x18},
  287. {0x4641, 0x0D},
  288. {0x465B, 0x12},
  289. {0x465F, 0x11},
  290. {0x4663, 0x11},
  291. {0x4667, 0x0F},
  292. {0x466F, 0x0F},
  293. {0x470E, 0x09},
  294. {0x4909, 0xAB},
  295. {0x490B, 0x95},
  296. {0x4915, 0x5D},
  297. {0x4A5F, 0xFF},
  298. {0x4A61, 0xFF},
  299. {0x4A73, 0x62},
  300. {0x4A85, 0x00},
  301. {0x4A87, 0xFF},
  302. /* embedded data */
  303. {0x5041, 0x04},
  304. {0x583C, 0x04},
  305. {0x620E, 0x04},
  306. {0x6EB2, 0x01},
  307. {0x6EB3, 0x00},
  308. {0x9300, 0x02},
  309. /* imagequality */
  310. /* HDR setting */
  311. {0x3001, 0x07},
  312. {0x6D12, 0x3F},
  313. {0x6D13, 0xFF},
  314. {0x9344, 0x03},
  315. {0x9706, 0x10},
  316. {0x9707, 0x03},
  317. {0x9708, 0x03},
  318. {0x9E04, 0x01},
  319. {0x9E05, 0x00},
  320. {0x9E0C, 0x01},
  321. {0x9E0D, 0x02},
  322. {0x9E24, 0x00},
  323. {0x9E25, 0x8C},
  324. {0x9E26, 0x00},
  325. {0x9E27, 0x94},
  326. {0x9E28, 0x00},
  327. {0x9E29, 0x96},
  328. /* CNR parameter setting */
  329. {0x69DB, 0x01},
  330. /* Moire reduction */
  331. {0x6957, 0x01},
  332. /* image enhancement */
  333. {0x6987, 0x17},
  334. {0x698A, 0x03},
  335. {0x698B, 0x03},
  336. /* white balanace */
  337. {0x0B8E, 0x01},
  338. {0x0B8F, 0x00},
  339. {0x0B90, 0x01},
  340. {0x0B91, 0x00},
  341. {0x0B92, 0x01},
  342. {0x0B93, 0x00},
  343. {0x0B94, 0x01},
  344. {0x0B95, 0x00},
  345. /* ATR setting */
  346. {0x6E50, 0x00},
  347. {0x6E51, 0x32},
  348. {0x9340, 0x00},
  349. {0x9341, 0x3C},
  350. {0x9342, 0x03},
  351. {0x9343, 0xFF},
  352. {IMX214_TABLE_END, 0x00}
  353. };
  354. /*
  355. * Declare modes in order, from biggest
  356. * to smallest height.
  357. */
  358. static const struct imx214_mode {
  359. u32 width;
  360. u32 height;
  361. const struct reg_8 *reg_table;
  362. } imx214_modes[] = {
  363. {
  364. .width = 4096,
  365. .height = 2304,
  366. .reg_table = mode_4096x2304,
  367. },
  368. {
  369. .width = 1920,
  370. .height = 1080,
  371. .reg_table = mode_1920x1080,
  372. },
  373. };
  374. static inline struct imx214 *to_imx214(struct v4l2_subdev *sd)
  375. {
  376. return container_of(sd, struct imx214, sd);
  377. }
  378. static int __maybe_unused imx214_power_on(struct device *dev)
  379. {
  380. struct i2c_client *client = to_i2c_client(dev);
  381. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  382. struct imx214 *imx214 = to_imx214(sd);
  383. int ret;
  384. ret = regulator_bulk_enable(IMX214_NUM_SUPPLIES, imx214->supplies);
  385. if (ret < 0) {
  386. dev_err(imx214->dev, "failed to enable regulators: %d\n", ret);
  387. return ret;
  388. }
  389. usleep_range(2000, 3000);
  390. ret = clk_prepare_enable(imx214->xclk);
  391. if (ret < 0) {
  392. regulator_bulk_disable(IMX214_NUM_SUPPLIES, imx214->supplies);
  393. dev_err(imx214->dev, "clk prepare enable failed\n");
  394. return ret;
  395. }
  396. gpiod_set_value_cansleep(imx214->enable_gpio, 1);
  397. usleep_range(12000, 15000);
  398. return 0;
  399. }
  400. static int __maybe_unused imx214_power_off(struct device *dev)
  401. {
  402. struct i2c_client *client = to_i2c_client(dev);
  403. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  404. struct imx214 *imx214 = to_imx214(sd);
  405. gpiod_set_value_cansleep(imx214->enable_gpio, 0);
  406. clk_disable_unprepare(imx214->xclk);
  407. regulator_bulk_disable(IMX214_NUM_SUPPLIES, imx214->supplies);
  408. usleep_range(10, 20);
  409. return 0;
  410. }
  411. static int imx214_enum_mbus_code(struct v4l2_subdev *sd,
  412. struct v4l2_subdev_state *sd_state,
  413. struct v4l2_subdev_mbus_code_enum *code)
  414. {
  415. if (code->index > 0)
  416. return -EINVAL;
  417. code->code = IMX214_MBUS_CODE;
  418. return 0;
  419. }
  420. static int imx214_enum_frame_size(struct v4l2_subdev *subdev,
  421. struct v4l2_subdev_state *sd_state,
  422. struct v4l2_subdev_frame_size_enum *fse)
  423. {
  424. if (fse->code != IMX214_MBUS_CODE)
  425. return -EINVAL;
  426. if (fse->index >= ARRAY_SIZE(imx214_modes))
  427. return -EINVAL;
  428. fse->min_width = fse->max_width = imx214_modes[fse->index].width;
  429. fse->min_height = fse->max_height = imx214_modes[fse->index].height;
  430. return 0;
  431. }
  432. #ifdef CONFIG_VIDEO_ADV_DEBUG
  433. static int imx214_s_register(struct v4l2_subdev *subdev,
  434. const struct v4l2_dbg_register *reg)
  435. {
  436. struct imx214 *imx214 = container_of(subdev, struct imx214, sd);
  437. return regmap_write(imx214->regmap, reg->reg, reg->val);
  438. }
  439. static int imx214_g_register(struct v4l2_subdev *subdev,
  440. struct v4l2_dbg_register *reg)
  441. {
  442. struct imx214 *imx214 = container_of(subdev, struct imx214, sd);
  443. unsigned int aux;
  444. int ret;
  445. reg->size = 1;
  446. ret = regmap_read(imx214->regmap, reg->reg, &aux);
  447. reg->val = aux;
  448. return ret;
  449. }
  450. #endif
  451. static const struct v4l2_subdev_core_ops imx214_core_ops = {
  452. #ifdef CONFIG_VIDEO_ADV_DEBUG
  453. .g_register = imx214_g_register,
  454. .s_register = imx214_s_register,
  455. #endif
  456. };
  457. static struct v4l2_mbus_framefmt *
  458. __imx214_get_pad_format(struct imx214 *imx214,
  459. struct v4l2_subdev_state *sd_state,
  460. unsigned int pad,
  461. enum v4l2_subdev_format_whence which)
  462. {
  463. switch (which) {
  464. case V4L2_SUBDEV_FORMAT_TRY:
  465. return v4l2_subdev_get_try_format(&imx214->sd, sd_state, pad);
  466. case V4L2_SUBDEV_FORMAT_ACTIVE:
  467. return &imx214->fmt;
  468. default:
  469. return NULL;
  470. }
  471. }
  472. static int imx214_get_format(struct v4l2_subdev *sd,
  473. struct v4l2_subdev_state *sd_state,
  474. struct v4l2_subdev_format *format)
  475. {
  476. struct imx214 *imx214 = to_imx214(sd);
  477. mutex_lock(&imx214->mutex);
  478. format->format = *__imx214_get_pad_format(imx214, sd_state,
  479. format->pad,
  480. format->which);
  481. mutex_unlock(&imx214->mutex);
  482. return 0;
  483. }
  484. static struct v4l2_rect *
  485. __imx214_get_pad_crop(struct imx214 *imx214,
  486. struct v4l2_subdev_state *sd_state,
  487. unsigned int pad, enum v4l2_subdev_format_whence which)
  488. {
  489. switch (which) {
  490. case V4L2_SUBDEV_FORMAT_TRY:
  491. return v4l2_subdev_get_try_crop(&imx214->sd, sd_state, pad);
  492. case V4L2_SUBDEV_FORMAT_ACTIVE:
  493. return &imx214->crop;
  494. default:
  495. return NULL;
  496. }
  497. }
  498. static int imx214_set_format(struct v4l2_subdev *sd,
  499. struct v4l2_subdev_state *sd_state,
  500. struct v4l2_subdev_format *format)
  501. {
  502. struct imx214 *imx214 = to_imx214(sd);
  503. struct v4l2_mbus_framefmt *__format;
  504. struct v4l2_rect *__crop;
  505. const struct imx214_mode *mode;
  506. mutex_lock(&imx214->mutex);
  507. __crop = __imx214_get_pad_crop(imx214, sd_state, format->pad,
  508. format->which);
  509. mode = v4l2_find_nearest_size(imx214_modes,
  510. ARRAY_SIZE(imx214_modes), width, height,
  511. format->format.width,
  512. format->format.height);
  513. __crop->width = mode->width;
  514. __crop->height = mode->height;
  515. __format = __imx214_get_pad_format(imx214, sd_state, format->pad,
  516. format->which);
  517. __format->width = __crop->width;
  518. __format->height = __crop->height;
  519. __format->code = IMX214_MBUS_CODE;
  520. __format->field = V4L2_FIELD_NONE;
  521. __format->colorspace = V4L2_COLORSPACE_SRGB;
  522. __format->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(__format->colorspace);
  523. __format->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
  524. __format->colorspace, __format->ycbcr_enc);
  525. __format->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(__format->colorspace);
  526. format->format = *__format;
  527. mutex_unlock(&imx214->mutex);
  528. return 0;
  529. }
  530. static int imx214_get_selection(struct v4l2_subdev *sd,
  531. struct v4l2_subdev_state *sd_state,
  532. struct v4l2_subdev_selection *sel)
  533. {
  534. struct imx214 *imx214 = to_imx214(sd);
  535. if (sel->target != V4L2_SEL_TGT_CROP)
  536. return -EINVAL;
  537. mutex_lock(&imx214->mutex);
  538. sel->r = *__imx214_get_pad_crop(imx214, sd_state, sel->pad,
  539. sel->which);
  540. mutex_unlock(&imx214->mutex);
  541. return 0;
  542. }
  543. static int imx214_entity_init_cfg(struct v4l2_subdev *subdev,
  544. struct v4l2_subdev_state *sd_state)
  545. {
  546. struct v4l2_subdev_format fmt = { };
  547. fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  548. fmt.format.width = imx214_modes[0].width;
  549. fmt.format.height = imx214_modes[0].height;
  550. imx214_set_format(subdev, sd_state, &fmt);
  551. return 0;
  552. }
  553. static int imx214_set_ctrl(struct v4l2_ctrl *ctrl)
  554. {
  555. struct imx214 *imx214 = container_of(ctrl->handler,
  556. struct imx214, ctrls);
  557. u8 vals[2];
  558. int ret;
  559. /*
  560. * Applying V4L2 control value only happens
  561. * when power is up for streaming
  562. */
  563. if (!pm_runtime_get_if_in_use(imx214->dev))
  564. return 0;
  565. switch (ctrl->id) {
  566. case V4L2_CID_EXPOSURE:
  567. vals[1] = ctrl->val;
  568. vals[0] = ctrl->val >> 8;
  569. ret = regmap_bulk_write(imx214->regmap, 0x202, vals, 2);
  570. if (ret < 0)
  571. dev_err(imx214->dev, "Error %d\n", ret);
  572. ret = 0;
  573. break;
  574. default:
  575. ret = -EINVAL;
  576. }
  577. pm_runtime_put(imx214->dev);
  578. return ret;
  579. }
  580. static const struct v4l2_ctrl_ops imx214_ctrl_ops = {
  581. .s_ctrl = imx214_set_ctrl,
  582. };
  583. #define MAX_CMD 4
  584. static int imx214_write_table(struct imx214 *imx214,
  585. const struct reg_8 table[])
  586. {
  587. u8 vals[MAX_CMD];
  588. int i;
  589. int ret;
  590. for (; table->addr != IMX214_TABLE_END ; table++) {
  591. if (table->addr == IMX214_TABLE_WAIT_MS) {
  592. usleep_range(table->val * 1000,
  593. table->val * 1000 + 500);
  594. continue;
  595. }
  596. for (i = 0; i < MAX_CMD; i++) {
  597. if (table[i].addr != (table[0].addr + i))
  598. break;
  599. vals[i] = table[i].val;
  600. }
  601. ret = regmap_bulk_write(imx214->regmap, table->addr, vals, i);
  602. if (ret) {
  603. dev_err(imx214->dev, "write_table error: %d\n", ret);
  604. return ret;
  605. }
  606. table += i - 1;
  607. }
  608. return 0;
  609. }
  610. static int imx214_start_streaming(struct imx214 *imx214)
  611. {
  612. const struct imx214_mode *mode;
  613. int ret;
  614. mutex_lock(&imx214->mutex);
  615. ret = imx214_write_table(imx214, mode_table_common);
  616. if (ret < 0) {
  617. dev_err(imx214->dev, "could not sent common table %d\n", ret);
  618. goto error;
  619. }
  620. mode = v4l2_find_nearest_size(imx214_modes,
  621. ARRAY_SIZE(imx214_modes), width, height,
  622. imx214->fmt.width, imx214->fmt.height);
  623. ret = imx214_write_table(imx214, mode->reg_table);
  624. if (ret < 0) {
  625. dev_err(imx214->dev, "could not sent mode table %d\n", ret);
  626. goto error;
  627. }
  628. ret = __v4l2_ctrl_handler_setup(&imx214->ctrls);
  629. if (ret < 0) {
  630. dev_err(imx214->dev, "could not sync v4l2 controls\n");
  631. goto error;
  632. }
  633. ret = regmap_write(imx214->regmap, 0x100, 1);
  634. if (ret < 0) {
  635. dev_err(imx214->dev, "could not sent start table %d\n", ret);
  636. goto error;
  637. }
  638. mutex_unlock(&imx214->mutex);
  639. return 0;
  640. error:
  641. mutex_unlock(&imx214->mutex);
  642. return ret;
  643. }
  644. static int imx214_stop_streaming(struct imx214 *imx214)
  645. {
  646. int ret;
  647. ret = regmap_write(imx214->regmap, 0x100, 0);
  648. if (ret < 0)
  649. dev_err(imx214->dev, "could not sent stop table %d\n", ret);
  650. return ret;
  651. }
  652. static int imx214_s_stream(struct v4l2_subdev *subdev, int enable)
  653. {
  654. struct imx214 *imx214 = to_imx214(subdev);
  655. int ret;
  656. if (imx214->streaming == enable)
  657. return 0;
  658. if (enable) {
  659. ret = pm_runtime_resume_and_get(imx214->dev);
  660. if (ret < 0)
  661. return ret;
  662. ret = imx214_start_streaming(imx214);
  663. if (ret < 0)
  664. goto err_rpm_put;
  665. } else {
  666. ret = imx214_stop_streaming(imx214);
  667. if (ret < 0)
  668. goto err_rpm_put;
  669. pm_runtime_put(imx214->dev);
  670. }
  671. imx214->streaming = enable;
  672. return 0;
  673. err_rpm_put:
  674. pm_runtime_put(imx214->dev);
  675. return ret;
  676. }
  677. static int imx214_g_frame_interval(struct v4l2_subdev *subdev,
  678. struct v4l2_subdev_frame_interval *fival)
  679. {
  680. fival->interval.numerator = 1;
  681. fival->interval.denominator = IMX214_FPS;
  682. return 0;
  683. }
  684. static int imx214_enum_frame_interval(struct v4l2_subdev *subdev,
  685. struct v4l2_subdev_state *sd_state,
  686. struct v4l2_subdev_frame_interval_enum *fie)
  687. {
  688. const struct imx214_mode *mode;
  689. if (fie->index != 0)
  690. return -EINVAL;
  691. mode = v4l2_find_nearest_size(imx214_modes,
  692. ARRAY_SIZE(imx214_modes), width, height,
  693. fie->width, fie->height);
  694. fie->code = IMX214_MBUS_CODE;
  695. fie->width = mode->width;
  696. fie->height = mode->height;
  697. fie->interval.numerator = 1;
  698. fie->interval.denominator = IMX214_FPS;
  699. return 0;
  700. }
  701. static const struct v4l2_subdev_video_ops imx214_video_ops = {
  702. .s_stream = imx214_s_stream,
  703. .g_frame_interval = imx214_g_frame_interval,
  704. .s_frame_interval = imx214_g_frame_interval,
  705. };
  706. static const struct v4l2_subdev_pad_ops imx214_subdev_pad_ops = {
  707. .enum_mbus_code = imx214_enum_mbus_code,
  708. .enum_frame_size = imx214_enum_frame_size,
  709. .enum_frame_interval = imx214_enum_frame_interval,
  710. .get_fmt = imx214_get_format,
  711. .set_fmt = imx214_set_format,
  712. .get_selection = imx214_get_selection,
  713. .init_cfg = imx214_entity_init_cfg,
  714. };
  715. static const struct v4l2_subdev_ops imx214_subdev_ops = {
  716. .core = &imx214_core_ops,
  717. .video = &imx214_video_ops,
  718. .pad = &imx214_subdev_pad_ops,
  719. };
  720. static const struct regmap_config sensor_regmap_config = {
  721. .reg_bits = 16,
  722. .val_bits = 8,
  723. .cache_type = REGCACHE_RBTREE,
  724. };
  725. static int imx214_get_regulators(struct device *dev, struct imx214 *imx214)
  726. {
  727. unsigned int i;
  728. for (i = 0; i < IMX214_NUM_SUPPLIES; i++)
  729. imx214->supplies[i].supply = imx214_supply_name[i];
  730. return devm_regulator_bulk_get(dev, IMX214_NUM_SUPPLIES,
  731. imx214->supplies);
  732. }
  733. static int imx214_parse_fwnode(struct device *dev)
  734. {
  735. struct fwnode_handle *endpoint;
  736. struct v4l2_fwnode_endpoint bus_cfg = {
  737. .bus_type = V4L2_MBUS_CSI2_DPHY,
  738. };
  739. unsigned int i;
  740. int ret;
  741. endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
  742. if (!endpoint) {
  743. dev_err(dev, "endpoint node not found\n");
  744. return -EINVAL;
  745. }
  746. ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
  747. if (ret) {
  748. dev_err(dev, "parsing endpoint node failed\n");
  749. goto done;
  750. }
  751. for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
  752. if (bus_cfg.link_frequencies[i] == IMX214_DEFAULT_LINK_FREQ)
  753. break;
  754. if (i == bus_cfg.nr_of_link_frequencies) {
  755. dev_err(dev, "link-frequencies %d not supported, Please review your DT\n",
  756. IMX214_DEFAULT_LINK_FREQ);
  757. ret = -EINVAL;
  758. goto done;
  759. }
  760. done:
  761. v4l2_fwnode_endpoint_free(&bus_cfg);
  762. fwnode_handle_put(endpoint);
  763. return ret;
  764. }
  765. static int __maybe_unused imx214_suspend(struct device *dev)
  766. {
  767. struct i2c_client *client = to_i2c_client(dev);
  768. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  769. struct imx214 *imx214 = to_imx214(sd);
  770. if (imx214->streaming)
  771. imx214_stop_streaming(imx214);
  772. return 0;
  773. }
  774. static int __maybe_unused imx214_resume(struct device *dev)
  775. {
  776. struct i2c_client *client = to_i2c_client(dev);
  777. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  778. struct imx214 *imx214 = to_imx214(sd);
  779. int ret;
  780. if (imx214->streaming) {
  781. ret = imx214_start_streaming(imx214);
  782. if (ret)
  783. goto error;
  784. }
  785. return 0;
  786. error:
  787. imx214_stop_streaming(imx214);
  788. imx214->streaming = 0;
  789. return ret;
  790. }
  791. static int imx214_probe(struct i2c_client *client)
  792. {
  793. struct device *dev = &client->dev;
  794. struct imx214 *imx214;
  795. static const s64 link_freq[] = {
  796. IMX214_DEFAULT_LINK_FREQ,
  797. };
  798. static const struct v4l2_area unit_size = {
  799. .width = 1120,
  800. .height = 1120,
  801. };
  802. int ret;
  803. ret = imx214_parse_fwnode(dev);
  804. if (ret)
  805. return ret;
  806. imx214 = devm_kzalloc(dev, sizeof(*imx214), GFP_KERNEL);
  807. if (!imx214)
  808. return -ENOMEM;
  809. imx214->dev = dev;
  810. imx214->xclk = devm_clk_get(dev, NULL);
  811. if (IS_ERR(imx214->xclk)) {
  812. dev_err(dev, "could not get xclk");
  813. return PTR_ERR(imx214->xclk);
  814. }
  815. ret = clk_set_rate(imx214->xclk, IMX214_DEFAULT_CLK_FREQ);
  816. if (ret) {
  817. dev_err(dev, "could not set xclk frequency\n");
  818. return ret;
  819. }
  820. ret = imx214_get_regulators(dev, imx214);
  821. if (ret < 0) {
  822. dev_err(dev, "cannot get regulators\n");
  823. return ret;
  824. }
  825. imx214->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
  826. if (IS_ERR(imx214->enable_gpio)) {
  827. dev_err(dev, "cannot get enable gpio\n");
  828. return PTR_ERR(imx214->enable_gpio);
  829. }
  830. imx214->regmap = devm_regmap_init_i2c(client, &sensor_regmap_config);
  831. if (IS_ERR(imx214->regmap)) {
  832. dev_err(dev, "regmap init failed\n");
  833. return PTR_ERR(imx214->regmap);
  834. }
  835. v4l2_i2c_subdev_init(&imx214->sd, client, &imx214_subdev_ops);
  836. /*
  837. * Enable power initially, to avoid warnings
  838. * from clk_disable on power_off
  839. */
  840. imx214_power_on(imx214->dev);
  841. pm_runtime_set_active(imx214->dev);
  842. pm_runtime_enable(imx214->dev);
  843. pm_runtime_idle(imx214->dev);
  844. v4l2_ctrl_handler_init(&imx214->ctrls, 3);
  845. imx214->pixel_rate = v4l2_ctrl_new_std(&imx214->ctrls, NULL,
  846. V4L2_CID_PIXEL_RATE, 0,
  847. IMX214_DEFAULT_PIXEL_RATE, 1,
  848. IMX214_DEFAULT_PIXEL_RATE);
  849. imx214->link_freq = v4l2_ctrl_new_int_menu(&imx214->ctrls, NULL,
  850. V4L2_CID_LINK_FREQ,
  851. ARRAY_SIZE(link_freq) - 1,
  852. 0, link_freq);
  853. if (imx214->link_freq)
  854. imx214->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  855. /*
  856. * WARNING!
  857. * Values obtained reverse engineering blobs and/or devices.
  858. * Ranges and functionality might be wrong.
  859. *
  860. * Sony, please release some register set documentation for the
  861. * device.
  862. *
  863. * Yours sincerely, Ricardo.
  864. */
  865. imx214->exposure = v4l2_ctrl_new_std(&imx214->ctrls, &imx214_ctrl_ops,
  866. V4L2_CID_EXPOSURE,
  867. 0, 3184, 1, 0x0c70);
  868. imx214->unit_size = v4l2_ctrl_new_std_compound(&imx214->ctrls,
  869. NULL,
  870. V4L2_CID_UNIT_CELL_SIZE,
  871. v4l2_ctrl_ptr_create((void *)&unit_size));
  872. ret = imx214->ctrls.error;
  873. if (ret) {
  874. dev_err(&client->dev, "%s control init failed (%d)\n",
  875. __func__, ret);
  876. goto free_ctrl;
  877. }
  878. imx214->sd.ctrl_handler = &imx214->ctrls;
  879. mutex_init(&imx214->mutex);
  880. imx214->ctrls.lock = &imx214->mutex;
  881. imx214->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  882. imx214->pad.flags = MEDIA_PAD_FL_SOURCE;
  883. imx214->sd.dev = &client->dev;
  884. imx214->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  885. ret = media_entity_pads_init(&imx214->sd.entity, 1, &imx214->pad);
  886. if (ret < 0) {
  887. dev_err(dev, "could not register media entity\n");
  888. goto free_ctrl;
  889. }
  890. imx214_entity_init_cfg(&imx214->sd, NULL);
  891. ret = v4l2_async_register_subdev_sensor(&imx214->sd);
  892. if (ret < 0) {
  893. dev_err(dev, "could not register v4l2 device\n");
  894. goto free_entity;
  895. }
  896. return 0;
  897. free_entity:
  898. media_entity_cleanup(&imx214->sd.entity);
  899. free_ctrl:
  900. mutex_destroy(&imx214->mutex);
  901. v4l2_ctrl_handler_free(&imx214->ctrls);
  902. pm_runtime_disable(imx214->dev);
  903. return ret;
  904. }
  905. static void imx214_remove(struct i2c_client *client)
  906. {
  907. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  908. struct imx214 *imx214 = to_imx214(sd);
  909. v4l2_async_unregister_subdev(&imx214->sd);
  910. media_entity_cleanup(&imx214->sd.entity);
  911. v4l2_ctrl_handler_free(&imx214->ctrls);
  912. pm_runtime_disable(&client->dev);
  913. pm_runtime_set_suspended(&client->dev);
  914. mutex_destroy(&imx214->mutex);
  915. }
  916. static const struct of_device_id imx214_of_match[] = {
  917. { .compatible = "sony,imx214" },
  918. { }
  919. };
  920. MODULE_DEVICE_TABLE(of, imx214_of_match);
  921. static const struct dev_pm_ops imx214_pm_ops = {
  922. SET_SYSTEM_SLEEP_PM_OPS(imx214_suspend, imx214_resume)
  923. SET_RUNTIME_PM_OPS(imx214_power_off, imx214_power_on, NULL)
  924. };
  925. static struct i2c_driver imx214_i2c_driver = {
  926. .driver = {
  927. .of_match_table = imx214_of_match,
  928. .pm = &imx214_pm_ops,
  929. .name = "imx214",
  930. },
  931. .probe_new = imx214_probe,
  932. .remove = imx214_remove,
  933. };
  934. module_i2c_driver(imx214_i2c_driver);
  935. MODULE_DESCRIPTION("Sony IMX214 Camera driver");
  936. MODULE_AUTHOR("Ricardo Ribalda <[email protected]>");
  937. MODULE_LICENSE("GPL v2");