imx214.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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 <ribalda@kernel.org>
  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_pad_config *cfg,
  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_pad_config *cfg,
  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_pad_config *cfg,
  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, cfg, 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_pad_config *cfg,
  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, cfg, format->pad,
  479. format->which);
  480. mutex_unlock(&imx214->mutex);
  481. return 0;
  482. }
  483. static struct v4l2_rect *
  484. __imx214_get_pad_crop(struct imx214 *imx214, struct v4l2_subdev_pad_config *cfg,
  485. unsigned int pad, enum v4l2_subdev_format_whence which)
  486. {
  487. switch (which) {
  488. case V4L2_SUBDEV_FORMAT_TRY:
  489. return v4l2_subdev_get_try_crop(&imx214->sd, cfg, pad);
  490. case V4L2_SUBDEV_FORMAT_ACTIVE:
  491. return &imx214->crop;
  492. default:
  493. return NULL;
  494. }
  495. }
  496. static int imx214_set_format(struct v4l2_subdev *sd,
  497. struct v4l2_subdev_pad_config *cfg,
  498. struct v4l2_subdev_format *format)
  499. {
  500. struct imx214 *imx214 = to_imx214(sd);
  501. struct v4l2_mbus_framefmt *__format;
  502. struct v4l2_rect *__crop;
  503. const struct imx214_mode *mode;
  504. mutex_lock(&imx214->mutex);
  505. __crop = __imx214_get_pad_crop(imx214, cfg, format->pad, format->which);
  506. mode = v4l2_find_nearest_size(imx214_modes,
  507. ARRAY_SIZE(imx214_modes), width, height,
  508. format->format.width,
  509. format->format.height);
  510. __crop->width = mode->width;
  511. __crop->height = mode->height;
  512. __format = __imx214_get_pad_format(imx214, cfg, format->pad,
  513. format->which);
  514. __format->width = __crop->width;
  515. __format->height = __crop->height;
  516. __format->code = IMX214_MBUS_CODE;
  517. __format->field = V4L2_FIELD_NONE;
  518. __format->colorspace = V4L2_COLORSPACE_SRGB;
  519. __format->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(__format->colorspace);
  520. __format->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
  521. __format->colorspace, __format->ycbcr_enc);
  522. __format->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(__format->colorspace);
  523. format->format = *__format;
  524. mutex_unlock(&imx214->mutex);
  525. return 0;
  526. }
  527. static int imx214_get_selection(struct v4l2_subdev *sd,
  528. struct v4l2_subdev_pad_config *cfg,
  529. struct v4l2_subdev_selection *sel)
  530. {
  531. struct imx214 *imx214 = to_imx214(sd);
  532. if (sel->target != V4L2_SEL_TGT_CROP)
  533. return -EINVAL;
  534. mutex_lock(&imx214->mutex);
  535. sel->r = *__imx214_get_pad_crop(imx214, cfg, sel->pad,
  536. sel->which);
  537. mutex_unlock(&imx214->mutex);
  538. return 0;
  539. }
  540. static int imx214_entity_init_cfg(struct v4l2_subdev *subdev,
  541. struct v4l2_subdev_pad_config *cfg)
  542. {
  543. struct v4l2_subdev_format fmt = { };
  544. fmt.which = cfg ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  545. fmt.format.width = imx214_modes[0].width;
  546. fmt.format.height = imx214_modes[0].height;
  547. imx214_set_format(subdev, cfg, &fmt);
  548. return 0;
  549. }
  550. static int imx214_set_ctrl(struct v4l2_ctrl *ctrl)
  551. {
  552. struct imx214 *imx214 = container_of(ctrl->handler,
  553. struct imx214, ctrls);
  554. u8 vals[2];
  555. int ret;
  556. /*
  557. * Applying V4L2 control value only happens
  558. * when power is up for streaming
  559. */
  560. if (!pm_runtime_get_if_in_use(imx214->dev))
  561. return 0;
  562. switch (ctrl->id) {
  563. case V4L2_CID_EXPOSURE:
  564. vals[1] = ctrl->val;
  565. vals[0] = ctrl->val >> 8;
  566. ret = regmap_bulk_write(imx214->regmap, 0x202, vals, 2);
  567. if (ret < 0)
  568. dev_err(imx214->dev, "Error %d\n", ret);
  569. ret = 0;
  570. break;
  571. default:
  572. ret = -EINVAL;
  573. }
  574. pm_runtime_put(imx214->dev);
  575. return ret;
  576. }
  577. static const struct v4l2_ctrl_ops imx214_ctrl_ops = {
  578. .s_ctrl = imx214_set_ctrl,
  579. };
  580. #define MAX_CMD 4
  581. static int imx214_write_table(struct imx214 *imx214,
  582. const struct reg_8 table[])
  583. {
  584. u8 vals[MAX_CMD];
  585. int i;
  586. int ret;
  587. for (; table->addr != IMX214_TABLE_END ; table++) {
  588. if (table->addr == IMX214_TABLE_WAIT_MS) {
  589. usleep_range(table->val * 1000,
  590. table->val * 1000 + 500);
  591. continue;
  592. }
  593. for (i = 0; i < MAX_CMD; i++) {
  594. if (table[i].addr != (table[0].addr + i))
  595. break;
  596. vals[i] = table[i].val;
  597. }
  598. ret = regmap_bulk_write(imx214->regmap, table->addr, vals, i);
  599. if (ret) {
  600. dev_err(imx214->dev, "write_table error: %d\n", ret);
  601. return ret;
  602. }
  603. table += i - 1;
  604. }
  605. return 0;
  606. }
  607. static int imx214_start_streaming(struct imx214 *imx214)
  608. {
  609. const struct imx214_mode *mode;
  610. int ret;
  611. mutex_lock(&imx214->mutex);
  612. ret = imx214_write_table(imx214, mode_table_common);
  613. if (ret < 0) {
  614. dev_err(imx214->dev, "could not sent common table %d\n", ret);
  615. goto error;
  616. }
  617. mode = v4l2_find_nearest_size(imx214_modes,
  618. ARRAY_SIZE(imx214_modes), width, height,
  619. imx214->fmt.width, imx214->fmt.height);
  620. ret = imx214_write_table(imx214, mode->reg_table);
  621. if (ret < 0) {
  622. dev_err(imx214->dev, "could not sent mode table %d\n", ret);
  623. goto error;
  624. }
  625. ret = __v4l2_ctrl_handler_setup(&imx214->ctrls);
  626. if (ret < 0) {
  627. dev_err(imx214->dev, "could not sync v4l2 controls\n");
  628. goto error;
  629. }
  630. ret = regmap_write(imx214->regmap, 0x100, 1);
  631. if (ret < 0) {
  632. dev_err(imx214->dev, "could not sent start table %d\n", ret);
  633. goto error;
  634. }
  635. mutex_unlock(&imx214->mutex);
  636. return 0;
  637. error:
  638. mutex_unlock(&imx214->mutex);
  639. return ret;
  640. }
  641. static int imx214_stop_streaming(struct imx214 *imx214)
  642. {
  643. int ret;
  644. ret = regmap_write(imx214->regmap, 0x100, 0);
  645. if (ret < 0)
  646. dev_err(imx214->dev, "could not sent stop table %d\n", ret);
  647. return ret;
  648. }
  649. static int imx214_s_stream(struct v4l2_subdev *subdev, int enable)
  650. {
  651. struct imx214 *imx214 = to_imx214(subdev);
  652. int ret;
  653. if (imx214->streaming == enable)
  654. return 0;
  655. if (enable) {
  656. ret = pm_runtime_get_sync(imx214->dev);
  657. if (ret < 0) {
  658. pm_runtime_put_noidle(imx214->dev);
  659. return ret;
  660. }
  661. ret = imx214_start_streaming(imx214);
  662. if (ret < 0)
  663. goto err_rpm_put;
  664. } else {
  665. ret = imx214_stop_streaming(imx214);
  666. if (ret < 0)
  667. goto err_rpm_put;
  668. pm_runtime_put(imx214->dev);
  669. }
  670. imx214->streaming = enable;
  671. return 0;
  672. err_rpm_put:
  673. pm_runtime_put(imx214->dev);
  674. return ret;
  675. }
  676. static int imx214_g_frame_interval(struct v4l2_subdev *subdev,
  677. struct v4l2_subdev_frame_interval *fival)
  678. {
  679. fival->interval.numerator = 1;
  680. fival->interval.denominator = IMX214_FPS;
  681. return 0;
  682. }
  683. static int imx214_enum_frame_interval(struct v4l2_subdev *subdev,
  684. struct v4l2_subdev_pad_config *cfg,
  685. struct v4l2_subdev_frame_interval_enum *fie)
  686. {
  687. const struct imx214_mode *mode;
  688. if (fie->index != 0)
  689. return -EINVAL;
  690. mode = v4l2_find_nearest_size(imx214_modes,
  691. ARRAY_SIZE(imx214_modes), width, height,
  692. fie->width, fie->height);
  693. fie->code = IMX214_MBUS_CODE;
  694. fie->width = mode->width;
  695. fie->height = mode->height;
  696. fie->interval.numerator = 1;
  697. fie->interval.denominator = IMX214_FPS;
  698. return 0;
  699. }
  700. static const struct v4l2_subdev_video_ops imx214_video_ops = {
  701. .s_stream = imx214_s_stream,
  702. .g_frame_interval = imx214_g_frame_interval,
  703. .s_frame_interval = imx214_g_frame_interval,
  704. };
  705. static const struct v4l2_subdev_pad_ops imx214_subdev_pad_ops = {
  706. .enum_mbus_code = imx214_enum_mbus_code,
  707. .enum_frame_size = imx214_enum_frame_size,
  708. .enum_frame_interval = imx214_enum_frame_interval,
  709. .get_fmt = imx214_get_format,
  710. .set_fmt = imx214_set_format,
  711. .get_selection = imx214_get_selection,
  712. .init_cfg = imx214_entity_init_cfg,
  713. };
  714. static const struct v4l2_subdev_ops imx214_subdev_ops = {
  715. .core = &imx214_core_ops,
  716. .video = &imx214_video_ops,
  717. .pad = &imx214_subdev_pad_ops,
  718. };
  719. static const struct regmap_config sensor_regmap_config = {
  720. .reg_bits = 16,
  721. .val_bits = 8,
  722. .cache_type = REGCACHE_RBTREE,
  723. };
  724. static int imx214_get_regulators(struct device *dev, struct imx214 *imx214)
  725. {
  726. unsigned int i;
  727. for (i = 0; i < IMX214_NUM_SUPPLIES; i++)
  728. imx214->supplies[i].supply = imx214_supply_name[i];
  729. return devm_regulator_bulk_get(dev, IMX214_NUM_SUPPLIES,
  730. imx214->supplies);
  731. }
  732. static int imx214_parse_fwnode(struct device *dev)
  733. {
  734. struct fwnode_handle *endpoint;
  735. struct v4l2_fwnode_endpoint bus_cfg = {
  736. .bus_type = V4L2_MBUS_CSI2_DPHY,
  737. };
  738. unsigned int i;
  739. int ret;
  740. endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
  741. if (!endpoint) {
  742. dev_err(dev, "endpoint node not found\n");
  743. return -EINVAL;
  744. }
  745. ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
  746. if (ret) {
  747. dev_err(dev, "parsing endpoint node failed\n");
  748. goto done;
  749. }
  750. for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
  751. if (bus_cfg.link_frequencies[i] == IMX214_DEFAULT_LINK_FREQ)
  752. break;
  753. if (i == bus_cfg.nr_of_link_frequencies) {
  754. dev_err(dev, "link-frequencies %d not supported, Please review your DT\n",
  755. IMX214_DEFAULT_LINK_FREQ);
  756. ret = -EINVAL;
  757. goto done;
  758. }
  759. done:
  760. v4l2_fwnode_endpoint_free(&bus_cfg);
  761. fwnode_handle_put(endpoint);
  762. return ret;
  763. }
  764. static int __maybe_unused imx214_suspend(struct device *dev)
  765. {
  766. struct i2c_client *client = to_i2c_client(dev);
  767. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  768. struct imx214 *imx214 = to_imx214(sd);
  769. if (imx214->streaming)
  770. imx214_stop_streaming(imx214);
  771. return 0;
  772. }
  773. static int __maybe_unused imx214_resume(struct device *dev)
  774. {
  775. struct i2c_client *client = to_i2c_client(dev);
  776. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  777. struct imx214 *imx214 = to_imx214(sd);
  778. int ret;
  779. if (imx214->streaming) {
  780. ret = imx214_start_streaming(imx214);
  781. if (ret)
  782. goto error;
  783. }
  784. return 0;
  785. error:
  786. imx214_stop_streaming(imx214);
  787. imx214->streaming = 0;
  788. return ret;
  789. }
  790. static int imx214_probe(struct i2c_client *client)
  791. {
  792. struct device *dev = &client->dev;
  793. struct imx214 *imx214;
  794. static const s64 link_freq[] = {
  795. IMX214_DEFAULT_LINK_FREQ,
  796. };
  797. static const struct v4l2_area unit_size = {
  798. .width = 1120,
  799. .height = 1120,
  800. };
  801. int ret;
  802. ret = imx214_parse_fwnode(dev);
  803. if (ret)
  804. return ret;
  805. imx214 = devm_kzalloc(dev, sizeof(*imx214), GFP_KERNEL);
  806. if (!imx214)
  807. return -ENOMEM;
  808. imx214->dev = dev;
  809. imx214->xclk = devm_clk_get(dev, NULL);
  810. if (IS_ERR(imx214->xclk)) {
  811. dev_err(dev, "could not get xclk");
  812. return PTR_ERR(imx214->xclk);
  813. }
  814. ret = clk_set_rate(imx214->xclk, IMX214_DEFAULT_CLK_FREQ);
  815. if (ret) {
  816. dev_err(dev, "could not set xclk frequency\n");
  817. return ret;
  818. }
  819. ret = imx214_get_regulators(dev, imx214);
  820. if (ret < 0) {
  821. dev_err(dev, "cannot get regulators\n");
  822. return ret;
  823. }
  824. imx214->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
  825. if (IS_ERR(imx214->enable_gpio)) {
  826. dev_err(dev, "cannot get enable gpio\n");
  827. return PTR_ERR(imx214->enable_gpio);
  828. }
  829. imx214->regmap = devm_regmap_init_i2c(client, &sensor_regmap_config);
  830. if (IS_ERR(imx214->regmap)) {
  831. dev_err(dev, "regmap init failed\n");
  832. return PTR_ERR(imx214->regmap);
  833. }
  834. v4l2_i2c_subdev_init(&imx214->sd, client, &imx214_subdev_ops);
  835. /*
  836. * Enable power initially, to avoid warnings
  837. * from clk_disable on power_off
  838. */
  839. imx214_power_on(imx214->dev);
  840. pm_runtime_set_active(imx214->dev);
  841. pm_runtime_enable(imx214->dev);
  842. pm_runtime_idle(imx214->dev);
  843. v4l2_ctrl_handler_init(&imx214->ctrls, 3);
  844. imx214->pixel_rate = v4l2_ctrl_new_std(&imx214->ctrls, NULL,
  845. V4L2_CID_PIXEL_RATE, 0,
  846. IMX214_DEFAULT_PIXEL_RATE, 1,
  847. IMX214_DEFAULT_PIXEL_RATE);
  848. imx214->link_freq = v4l2_ctrl_new_int_menu(&imx214->ctrls, NULL,
  849. V4L2_CID_LINK_FREQ,
  850. ARRAY_SIZE(link_freq) - 1,
  851. 0, link_freq);
  852. if (imx214->link_freq)
  853. imx214->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  854. /*
  855. * WARNING!
  856. * Values obtained reverse engineering blobs and/or devices.
  857. * Ranges and functionality might be wrong.
  858. *
  859. * Sony, please release some register set documentation for the
  860. * device.
  861. *
  862. * Yours sincerely, Ricardo.
  863. */
  864. imx214->exposure = v4l2_ctrl_new_std(&imx214->ctrls, &imx214_ctrl_ops,
  865. V4L2_CID_EXPOSURE,
  866. 0, 3184, 1, 0x0c70);
  867. imx214->unit_size = v4l2_ctrl_new_std_compound(&imx214->ctrls,
  868. NULL,
  869. V4L2_CID_UNIT_CELL_SIZE,
  870. v4l2_ctrl_ptr_create((void *)&unit_size));
  871. ret = imx214->ctrls.error;
  872. if (ret) {
  873. dev_err(&client->dev, "%s control init failed (%d)\n",
  874. __func__, ret);
  875. goto free_ctrl;
  876. }
  877. imx214->sd.ctrl_handler = &imx214->ctrls;
  878. mutex_init(&imx214->mutex);
  879. imx214->ctrls.lock = &imx214->mutex;
  880. imx214->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  881. imx214->pad.flags = MEDIA_PAD_FL_SOURCE;
  882. imx214->sd.dev = &client->dev;
  883. imx214->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  884. ret = media_entity_pads_init(&imx214->sd.entity, 1, &imx214->pad);
  885. if (ret < 0) {
  886. dev_err(dev, "could not register media entity\n");
  887. goto free_ctrl;
  888. }
  889. imx214_entity_init_cfg(&imx214->sd, NULL);
  890. ret = v4l2_async_register_subdev_sensor_common(&imx214->sd);
  891. if (ret < 0) {
  892. dev_err(dev, "could not register v4l2 device\n");
  893. goto free_entity;
  894. }
  895. return 0;
  896. free_entity:
  897. media_entity_cleanup(&imx214->sd.entity);
  898. free_ctrl:
  899. mutex_destroy(&imx214->mutex);
  900. v4l2_ctrl_handler_free(&imx214->ctrls);
  901. pm_runtime_disable(imx214->dev);
  902. return ret;
  903. }
  904. static int imx214_remove(struct i2c_client *client)
  905. {
  906. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  907. struct imx214 *imx214 = to_imx214(sd);
  908. v4l2_async_unregister_subdev(&imx214->sd);
  909. media_entity_cleanup(&imx214->sd.entity);
  910. v4l2_ctrl_handler_free(&imx214->ctrls);
  911. pm_runtime_disable(&client->dev);
  912. pm_runtime_set_suspended(&client->dev);
  913. mutex_destroy(&imx214->mutex);
  914. return 0;
  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 <ribalda@kernel.org>");
  937. MODULE_LICENSE("GPL v2");