ov7740.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2017 Microchip Corporation.
  3. #include <linux/clk.h>
  4. #include <linux/delay.h>
  5. #include <linux/gpio/consumer.h>
  6. #include <linux/i2c.h>
  7. #include <linux/module.h>
  8. #include <linux/pm_runtime.h>
  9. #include <linux/regmap.h>
  10. #include <media/v4l2-ctrls.h>
  11. #include <media/v4l2-event.h>
  12. #include <media/v4l2-image-sizes.h>
  13. #include <media/v4l2-subdev.h>
  14. #define REG_OUTSIZE_LSB 0x34
  15. /* OV7740 register tables */
  16. #define REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
  17. #define REG_BGAIN 0x01 /* blue gain */
  18. #define REG_RGAIN 0x02 /* red gain */
  19. #define REG_GGAIN 0x03 /* green gain */
  20. #define REG_REG04 0x04 /* analog setting, dont change*/
  21. #define REG_BAVG 0x05 /* b channel average */
  22. #define REG_GAVG 0x06 /* g channel average */
  23. #define REG_RAVG 0x07 /* r channel average */
  24. #define REG_REG0C 0x0C /* filp enable */
  25. #define REG0C_IMG_FLIP 0x80
  26. #define REG0C_IMG_MIRROR 0x40
  27. #define REG_REG0E 0x0E /* blc line */
  28. #define REG_HAEC 0x0F /* auto exposure cntrl */
  29. #define REG_AEC 0x10 /* auto exposure cntrl */
  30. #define REG_CLK 0x11 /* Clock control */
  31. #define REG_REG55 0x55 /* Clock PLL DIV/PreDiv */
  32. #define REG_REG12 0x12
  33. #define REG_REG13 0x13 /* auto/manual AGC, AEC, Write Balance*/
  34. #define REG13_AEC_EN 0x01
  35. #define REG13_AGC_EN 0x04
  36. #define REG_REG14 0x14
  37. #define REG_CTRL15 0x15
  38. #define REG15_GAIN_MSB 0x03
  39. #define REG_REG16 0x16
  40. #define REG_MIDH 0x1C /* manufacture id byte */
  41. #define REG_MIDL 0x1D /* manufacture id byre */
  42. #define REG_PIDH 0x0A /* Product ID MSB */
  43. #define REG_PIDL 0x0B /* Product ID LSB */
  44. #define REG_84 0x84 /* lots of stuff */
  45. #define REG_REG38 0x38 /* sub-addr */
  46. #define REG_AHSTART 0x17 /* Horiz start high bits */
  47. #define REG_AHSIZE 0x18
  48. #define REG_AVSTART 0x19 /* Vert start high bits */
  49. #define REG_AVSIZE 0x1A
  50. #define REG_PSHFT 0x1b /* Pixel delay after HREF */
  51. #define REG_HOUTSIZE 0x31
  52. #define REG_VOUTSIZE 0x32
  53. #define REG_HVSIZEOFF 0x33
  54. #define REG_REG34 0x34 /* DSP output size H/V LSB*/
  55. #define REG_ISP_CTRL00 0x80
  56. #define ISPCTRL00_AWB_EN 0x10
  57. #define ISPCTRL00_AWB_GAIN_EN 0x04
  58. #define REG_YGAIN 0xE2 /* ygain for contrast control */
  59. #define REG_YBRIGHT 0xE3
  60. #define REG_SGNSET 0xE4
  61. #define SGNSET_YBRIGHT_MASK 0x08
  62. #define REG_USAT 0xDD
  63. #define REG_VSAT 0xDE
  64. struct ov7740 {
  65. struct v4l2_subdev subdev;
  66. #if defined(CONFIG_MEDIA_CONTROLLER)
  67. struct media_pad pad;
  68. #endif
  69. struct v4l2_mbus_framefmt format;
  70. const struct ov7740_pixfmt *fmt; /* Current format */
  71. const struct ov7740_framesize *frmsize;
  72. struct regmap *regmap;
  73. struct clk *xvclk;
  74. struct v4l2_ctrl_handler ctrl_handler;
  75. struct {
  76. /* gain cluster */
  77. struct v4l2_ctrl *auto_gain;
  78. struct v4l2_ctrl *gain;
  79. };
  80. struct {
  81. struct v4l2_ctrl *auto_wb;
  82. struct v4l2_ctrl *blue_balance;
  83. struct v4l2_ctrl *red_balance;
  84. };
  85. struct {
  86. struct v4l2_ctrl *hflip;
  87. struct v4l2_ctrl *vflip;
  88. };
  89. struct {
  90. /* exposure cluster */
  91. struct v4l2_ctrl *auto_exposure;
  92. struct v4l2_ctrl *exposure;
  93. };
  94. struct {
  95. /* saturation/hue cluster */
  96. struct v4l2_ctrl *saturation;
  97. struct v4l2_ctrl *hue;
  98. };
  99. struct v4l2_ctrl *brightness;
  100. struct v4l2_ctrl *contrast;
  101. struct mutex mutex; /* To serialize asynchronus callbacks */
  102. bool streaming; /* Streaming on/off */
  103. struct gpio_desc *resetb_gpio;
  104. struct gpio_desc *pwdn_gpio;
  105. };
  106. struct ov7740_pixfmt {
  107. u32 mbus_code;
  108. enum v4l2_colorspace colorspace;
  109. const struct reg_sequence *regs;
  110. u32 reg_num;
  111. };
  112. struct ov7740_framesize {
  113. u16 width;
  114. u16 height;
  115. const struct reg_sequence *regs;
  116. u32 reg_num;
  117. };
  118. static const struct reg_sequence ov7740_vga[] = {
  119. {0x55, 0x40},
  120. {0x11, 0x02},
  121. {0xd5, 0x10},
  122. {0x0c, 0x12},
  123. {0x0d, 0x34},
  124. {0x17, 0x25},
  125. {0x18, 0xa0},
  126. {0x19, 0x03},
  127. {0x1a, 0xf0},
  128. {0x1b, 0x89},
  129. {0x22, 0x03},
  130. {0x29, 0x18},
  131. {0x2b, 0xf8},
  132. {0x2c, 0x01},
  133. {REG_HOUTSIZE, 0xa0},
  134. {REG_VOUTSIZE, 0xf0},
  135. {0x33, 0xc4},
  136. {REG_OUTSIZE_LSB, 0x0},
  137. {0x35, 0x05},
  138. {0x04, 0x60},
  139. {0x27, 0x80},
  140. {0x3d, 0x0f},
  141. {0x3e, 0x80},
  142. {0x3f, 0x40},
  143. {0x40, 0x7f},
  144. {0x41, 0x6a},
  145. {0x42, 0x29},
  146. {0x44, 0x22},
  147. {0x45, 0x41},
  148. {0x47, 0x02},
  149. {0x49, 0x64},
  150. {0x4a, 0xa1},
  151. {0x4b, 0x40},
  152. {0x4c, 0x1a},
  153. {0x4d, 0x50},
  154. {0x4e, 0x13},
  155. {0x64, 0x00},
  156. {0x67, 0x88},
  157. {0x68, 0x1a},
  158. {0x14, 0x28},
  159. {0x24, 0x3c},
  160. {0x25, 0x30},
  161. {0x26, 0x72},
  162. {0x50, 0x97},
  163. {0x51, 0x1f},
  164. {0x52, 0x00},
  165. {0x53, 0x00},
  166. {0x20, 0x00},
  167. {0x21, 0xcf},
  168. {0x50, 0x4b},
  169. {0x38, 0x14},
  170. {0xe9, 0x00},
  171. {0x56, 0x55},
  172. {0x57, 0xff},
  173. {0x58, 0xff},
  174. {0x59, 0xff},
  175. {0x5f, 0x04},
  176. {0xec, 0x00},
  177. {0x13, 0xff},
  178. {0x81, 0x3f},
  179. {0x82, 0x32},
  180. {0x38, 0x11},
  181. {0x84, 0x70},
  182. {0x85, 0x00},
  183. {0x86, 0x03},
  184. {0x87, 0x01},
  185. {0x88, 0x05},
  186. {0x89, 0x30},
  187. {0x8d, 0x30},
  188. {0x8f, 0x85},
  189. {0x93, 0x30},
  190. {0x95, 0x85},
  191. {0x99, 0x30},
  192. {0x9b, 0x85},
  193. {0x9c, 0x08},
  194. {0x9d, 0x12},
  195. {0x9e, 0x23},
  196. {0x9f, 0x45},
  197. {0xa0, 0x55},
  198. {0xa1, 0x64},
  199. {0xa2, 0x72},
  200. {0xa3, 0x7f},
  201. {0xa4, 0x8b},
  202. {0xa5, 0x95},
  203. {0xa6, 0xa7},
  204. {0xa7, 0xb5},
  205. {0xa8, 0xcb},
  206. {0xa9, 0xdd},
  207. {0xaa, 0xec},
  208. {0xab, 0x1a},
  209. {0xce, 0x78},
  210. {0xcf, 0x6e},
  211. {0xd0, 0x0a},
  212. {0xd1, 0x0c},
  213. {0xd2, 0x84},
  214. {0xd3, 0x90},
  215. {0xd4, 0x1e},
  216. {0x5a, 0x24},
  217. {0x5b, 0x1f},
  218. {0x5c, 0x88},
  219. {0x5d, 0x60},
  220. {0xac, 0x6e},
  221. {0xbe, 0xff},
  222. {0xbf, 0x00},
  223. {0x0f, 0x1d},
  224. {0x0f, 0x1f},
  225. };
  226. static const struct ov7740_framesize ov7740_framesizes[] = {
  227. {
  228. .width = VGA_WIDTH,
  229. .height = VGA_HEIGHT,
  230. .regs = ov7740_vga,
  231. .reg_num = ARRAY_SIZE(ov7740_vga),
  232. },
  233. };
  234. #ifdef CONFIG_VIDEO_ADV_DEBUG
  235. static int ov7740_get_register(struct v4l2_subdev *sd,
  236. struct v4l2_dbg_register *reg)
  237. {
  238. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  239. struct regmap *regmap = ov7740->regmap;
  240. unsigned int val = 0;
  241. int ret;
  242. ret = regmap_read(regmap, reg->reg & 0xff, &val);
  243. reg->val = val;
  244. reg->size = 1;
  245. return ret;
  246. }
  247. static int ov7740_set_register(struct v4l2_subdev *sd,
  248. const struct v4l2_dbg_register *reg)
  249. {
  250. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  251. struct regmap *regmap = ov7740->regmap;
  252. regmap_write(regmap, reg->reg & 0xff, reg->val & 0xff);
  253. return 0;
  254. }
  255. #endif
  256. static int ov7740_set_power(struct ov7740 *ov7740, int on)
  257. {
  258. int ret;
  259. if (on) {
  260. ret = clk_prepare_enable(ov7740->xvclk);
  261. if (ret)
  262. return ret;
  263. if (ov7740->pwdn_gpio)
  264. gpiod_direction_output(ov7740->pwdn_gpio, 0);
  265. if (ov7740->resetb_gpio) {
  266. gpiod_set_value(ov7740->resetb_gpio, 1);
  267. usleep_range(500, 1000);
  268. gpiod_set_value(ov7740->resetb_gpio, 0);
  269. usleep_range(3000, 5000);
  270. }
  271. } else {
  272. clk_disable_unprepare(ov7740->xvclk);
  273. if (ov7740->pwdn_gpio)
  274. gpiod_direction_output(ov7740->pwdn_gpio, 0);
  275. }
  276. return 0;
  277. }
  278. static struct v4l2_subdev_core_ops ov7740_subdev_core_ops = {
  279. .log_status = v4l2_ctrl_subdev_log_status,
  280. #ifdef CONFIG_VIDEO_ADV_DEBUG
  281. .g_register = ov7740_get_register,
  282. .s_register = ov7740_set_register,
  283. #endif
  284. .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  285. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  286. };
  287. static int ov7740_set_white_balance(struct ov7740 *ov7740, int awb)
  288. {
  289. struct regmap *regmap = ov7740->regmap;
  290. unsigned int value;
  291. int ret;
  292. ret = regmap_read(regmap, REG_ISP_CTRL00, &value);
  293. if (!ret) {
  294. if (awb)
  295. value |= (ISPCTRL00_AWB_EN | ISPCTRL00_AWB_GAIN_EN);
  296. else
  297. value &= ~(ISPCTRL00_AWB_EN | ISPCTRL00_AWB_GAIN_EN);
  298. ret = regmap_write(regmap, REG_ISP_CTRL00, value);
  299. if (ret)
  300. return ret;
  301. }
  302. if (!awb) {
  303. ret = regmap_write(regmap, REG_BGAIN,
  304. ov7740->blue_balance->val);
  305. if (ret)
  306. return ret;
  307. ret = regmap_write(regmap, REG_RGAIN, ov7740->red_balance->val);
  308. if (ret)
  309. return ret;
  310. }
  311. return 0;
  312. }
  313. static int ov7740_set_saturation(struct regmap *regmap, int value)
  314. {
  315. int ret;
  316. ret = regmap_write(regmap, REG_USAT, (unsigned char)value);
  317. if (ret)
  318. return ret;
  319. return regmap_write(regmap, REG_VSAT, (unsigned char)value);
  320. }
  321. static int ov7740_set_gain(struct regmap *regmap, int value)
  322. {
  323. int ret;
  324. ret = regmap_write(regmap, REG_GAIN, value & 0xff);
  325. if (ret)
  326. return ret;
  327. ret = regmap_update_bits(regmap, REG_CTRL15,
  328. REG15_GAIN_MSB, (value >> 8) & 0x3);
  329. if (!ret)
  330. ret = regmap_update_bits(regmap, REG_REG13, REG13_AGC_EN, 0);
  331. return ret;
  332. }
  333. static int ov7740_set_autogain(struct regmap *regmap, int value)
  334. {
  335. unsigned int reg;
  336. int ret;
  337. ret = regmap_read(regmap, REG_REG13, &reg);
  338. if (ret)
  339. return ret;
  340. if (value)
  341. reg |= REG13_AGC_EN;
  342. else
  343. reg &= ~REG13_AGC_EN;
  344. return regmap_write(regmap, REG_REG13, reg);
  345. }
  346. static int ov7740_set_brightness(struct regmap *regmap, int value)
  347. {
  348. /* Turn off AEC/AGC */
  349. regmap_update_bits(regmap, REG_REG13, REG13_AEC_EN, 0);
  350. regmap_update_bits(regmap, REG_REG13, REG13_AGC_EN, 0);
  351. if (value >= 0) {
  352. regmap_write(regmap, REG_YBRIGHT, (unsigned char)value);
  353. regmap_update_bits(regmap, REG_SGNSET, SGNSET_YBRIGHT_MASK, 0);
  354. } else{
  355. regmap_write(regmap, REG_YBRIGHT, (unsigned char)(-value));
  356. regmap_update_bits(regmap, REG_SGNSET, SGNSET_YBRIGHT_MASK, 1);
  357. }
  358. return 0;
  359. }
  360. static int ov7740_set_contrast(struct regmap *regmap, int value)
  361. {
  362. return regmap_write(regmap, REG_YGAIN, (unsigned char)value);
  363. }
  364. static int ov7740_get_gain(struct ov7740 *ov7740, struct v4l2_ctrl *ctrl)
  365. {
  366. struct regmap *regmap = ov7740->regmap;
  367. unsigned int value0, value1;
  368. int ret;
  369. if (!ctrl->val)
  370. return 0;
  371. ret = regmap_read(regmap, REG_GAIN, &value0);
  372. if (ret)
  373. return ret;
  374. ret = regmap_read(regmap, REG_CTRL15, &value1);
  375. if (ret)
  376. return ret;
  377. ov7740->gain->val = (value1 << 8) | (value0 & 0xff);
  378. return 0;
  379. }
  380. static int ov7740_set_exp(struct regmap *regmap, int value)
  381. {
  382. int ret;
  383. /* Turn off AEC/AGC */
  384. ret = regmap_update_bits(regmap, REG_REG13,
  385. REG13_AEC_EN | REG13_AGC_EN, 0);
  386. if (ret)
  387. return ret;
  388. ret = regmap_write(regmap, REG_AEC, (unsigned char)value);
  389. if (ret)
  390. return ret;
  391. return regmap_write(regmap, REG_HAEC, (unsigned char)(value >> 8));
  392. }
  393. static int ov7740_set_autoexp(struct regmap *regmap,
  394. enum v4l2_exposure_auto_type value)
  395. {
  396. unsigned int reg;
  397. int ret;
  398. ret = regmap_read(regmap, REG_REG13, &reg);
  399. if (!ret) {
  400. if (value == V4L2_EXPOSURE_AUTO)
  401. reg |= (REG13_AEC_EN | REG13_AGC_EN);
  402. else
  403. reg &= ~(REG13_AEC_EN | REG13_AGC_EN);
  404. ret = regmap_write(regmap, REG_REG13, reg);
  405. }
  406. return ret;
  407. }
  408. static int ov7740_get_volatile_ctrl(struct v4l2_ctrl *ctrl)
  409. {
  410. struct ov7740 *ov7740 = container_of(ctrl->handler,
  411. struct ov7740, ctrl_handler);
  412. int ret;
  413. switch (ctrl->id) {
  414. case V4L2_CID_AUTOGAIN:
  415. ret = ov7740_get_gain(ov7740, ctrl);
  416. break;
  417. default:
  418. ret = -EINVAL;
  419. break;
  420. }
  421. return ret;
  422. }
  423. static int ov7740_set_ctrl(struct v4l2_ctrl *ctrl)
  424. {
  425. struct ov7740 *ov7740 = container_of(ctrl->handler,
  426. struct ov7740, ctrl_handler);
  427. struct i2c_client *client = v4l2_get_subdevdata(&ov7740->subdev);
  428. struct regmap *regmap = ov7740->regmap;
  429. int ret;
  430. u8 val = 0;
  431. if (pm_runtime_get_if_in_use(&client->dev) <= 0)
  432. return 0;
  433. switch (ctrl->id) {
  434. case V4L2_CID_AUTO_WHITE_BALANCE:
  435. ret = ov7740_set_white_balance(ov7740, ctrl->val);
  436. break;
  437. case V4L2_CID_SATURATION:
  438. ret = ov7740_set_saturation(regmap, ctrl->val);
  439. break;
  440. case V4L2_CID_BRIGHTNESS:
  441. ret = ov7740_set_brightness(regmap, ctrl->val);
  442. break;
  443. case V4L2_CID_CONTRAST:
  444. ret = ov7740_set_contrast(regmap, ctrl->val);
  445. break;
  446. case V4L2_CID_VFLIP:
  447. ret = regmap_update_bits(regmap, REG_REG0C,
  448. REG0C_IMG_FLIP, val);
  449. break;
  450. case V4L2_CID_HFLIP:
  451. val = ctrl->val ? REG0C_IMG_MIRROR : 0x00;
  452. ret = regmap_update_bits(regmap, REG_REG0C,
  453. REG0C_IMG_MIRROR, val);
  454. break;
  455. case V4L2_CID_AUTOGAIN:
  456. if (!ctrl->val)
  457. return ov7740_set_gain(regmap, ov7740->gain->val);
  458. ret = ov7740_set_autogain(regmap, ctrl->val);
  459. break;
  460. case V4L2_CID_EXPOSURE_AUTO:
  461. if (ctrl->val == V4L2_EXPOSURE_MANUAL)
  462. return ov7740_set_exp(regmap, ov7740->exposure->val);
  463. ret = ov7740_set_autoexp(regmap, ctrl->val);
  464. break;
  465. default:
  466. ret = -EINVAL;
  467. break;
  468. }
  469. pm_runtime_put(&client->dev);
  470. return ret;
  471. }
  472. static const struct v4l2_ctrl_ops ov7740_ctrl_ops = {
  473. .g_volatile_ctrl = ov7740_get_volatile_ctrl,
  474. .s_ctrl = ov7740_set_ctrl,
  475. };
  476. static int ov7740_start_streaming(struct ov7740 *ov7740)
  477. {
  478. int ret;
  479. if (ov7740->fmt) {
  480. ret = regmap_multi_reg_write(ov7740->regmap,
  481. ov7740->fmt->regs,
  482. ov7740->fmt->reg_num);
  483. if (ret)
  484. return ret;
  485. }
  486. if (ov7740->frmsize) {
  487. ret = regmap_multi_reg_write(ov7740->regmap,
  488. ov7740->frmsize->regs,
  489. ov7740->frmsize->reg_num);
  490. if (ret)
  491. return ret;
  492. }
  493. return __v4l2_ctrl_handler_setup(ov7740->subdev.ctrl_handler);
  494. }
  495. static int ov7740_set_stream(struct v4l2_subdev *sd, int enable)
  496. {
  497. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  498. struct i2c_client *client = v4l2_get_subdevdata(sd);
  499. int ret = 0;
  500. mutex_lock(&ov7740->mutex);
  501. if (ov7740->streaming == enable) {
  502. mutex_unlock(&ov7740->mutex);
  503. return 0;
  504. }
  505. if (enable) {
  506. ret = pm_runtime_get_sync(&client->dev);
  507. if (ret < 0) {
  508. pm_runtime_put_noidle(&client->dev);
  509. goto err_unlock;
  510. }
  511. ret = ov7740_start_streaming(ov7740);
  512. if (ret)
  513. goto err_rpm_put;
  514. } else {
  515. pm_runtime_put(&client->dev);
  516. }
  517. ov7740->streaming = enable;
  518. mutex_unlock(&ov7740->mutex);
  519. return ret;
  520. err_rpm_put:
  521. pm_runtime_put(&client->dev);
  522. err_unlock:
  523. mutex_unlock(&ov7740->mutex);
  524. return ret;
  525. }
  526. static int ov7740_g_frame_interval(struct v4l2_subdev *sd,
  527. struct v4l2_subdev_frame_interval *ival)
  528. {
  529. struct v4l2_fract *tpf = &ival->interval;
  530. tpf->numerator = 1;
  531. tpf->denominator = 60;
  532. return 0;
  533. }
  534. static int ov7740_s_frame_interval(struct v4l2_subdev *sd,
  535. struct v4l2_subdev_frame_interval *ival)
  536. {
  537. struct v4l2_fract *tpf = &ival->interval;
  538. tpf->numerator = 1;
  539. tpf->denominator = 60;
  540. return 0;
  541. }
  542. static struct v4l2_subdev_video_ops ov7740_subdev_video_ops = {
  543. .s_stream = ov7740_set_stream,
  544. .s_frame_interval = ov7740_s_frame_interval,
  545. .g_frame_interval = ov7740_g_frame_interval,
  546. };
  547. static const struct reg_sequence ov7740_format_yuyv[] = {
  548. {0x12, 0x00},
  549. {0x36, 0x3f},
  550. {0x80, 0x7f},
  551. {0x83, 0x01},
  552. };
  553. static const struct reg_sequence ov7740_format_bggr8[] = {
  554. {0x36, 0x2f},
  555. {0x80, 0x01},
  556. {0x83, 0x04},
  557. };
  558. static const struct ov7740_pixfmt ov7740_formats[] = {
  559. {
  560. .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
  561. .colorspace = V4L2_COLORSPACE_SRGB,
  562. .regs = ov7740_format_yuyv,
  563. .reg_num = ARRAY_SIZE(ov7740_format_yuyv),
  564. },
  565. {
  566. .mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
  567. .colorspace = V4L2_COLORSPACE_SRGB,
  568. .regs = ov7740_format_bggr8,
  569. .reg_num = ARRAY_SIZE(ov7740_format_bggr8),
  570. }
  571. };
  572. #define N_OV7740_FMTS ARRAY_SIZE(ov7740_formats)
  573. static int ov7740_enum_mbus_code(struct v4l2_subdev *sd,
  574. struct v4l2_subdev_pad_config *cfg,
  575. struct v4l2_subdev_mbus_code_enum *code)
  576. {
  577. if (code->pad || code->index >= N_OV7740_FMTS)
  578. return -EINVAL;
  579. code->code = ov7740_formats[code->index].mbus_code;
  580. return 0;
  581. }
  582. static int ov7740_enum_frame_interval(struct v4l2_subdev *sd,
  583. struct v4l2_subdev_pad_config *cfg,
  584. struct v4l2_subdev_frame_interval_enum *fie)
  585. {
  586. if (fie->pad)
  587. return -EINVAL;
  588. if (fie->index >= 1)
  589. return -EINVAL;
  590. if ((fie->width != VGA_WIDTH) || (fie->height != VGA_HEIGHT))
  591. return -EINVAL;
  592. fie->interval.numerator = 1;
  593. fie->interval.denominator = 60;
  594. return 0;
  595. }
  596. static int ov7740_enum_frame_size(struct v4l2_subdev *sd,
  597. struct v4l2_subdev_pad_config *cfg,
  598. struct v4l2_subdev_frame_size_enum *fse)
  599. {
  600. if (fse->pad)
  601. return -EINVAL;
  602. if (fse->index > 0)
  603. return -EINVAL;
  604. fse->min_width = fse->max_width = VGA_WIDTH;
  605. fse->min_height = fse->max_height = VGA_HEIGHT;
  606. return 0;
  607. }
  608. static int ov7740_try_fmt_internal(struct v4l2_subdev *sd,
  609. struct v4l2_mbus_framefmt *fmt,
  610. const struct ov7740_pixfmt **ret_fmt,
  611. const struct ov7740_framesize **ret_frmsize)
  612. {
  613. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  614. const struct ov7740_framesize *fsize = &ov7740_framesizes[0];
  615. int index, i;
  616. for (index = 0; index < N_OV7740_FMTS; index++) {
  617. if (ov7740_formats[index].mbus_code == fmt->code)
  618. break;
  619. }
  620. if (index >= N_OV7740_FMTS) {
  621. /* default to first format */
  622. index = 0;
  623. fmt->code = ov7740_formats[0].mbus_code;
  624. }
  625. if (ret_fmt != NULL)
  626. *ret_fmt = ov7740_formats + index;
  627. for (i = 0; i < ARRAY_SIZE(ov7740_framesizes); i++) {
  628. if ((fsize->width >= fmt->width) &&
  629. (fsize->height >= fmt->height)) {
  630. fmt->width = fsize->width;
  631. fmt->height = fsize->height;
  632. break;
  633. }
  634. fsize++;
  635. }
  636. if (ret_frmsize != NULL)
  637. *ret_frmsize = fsize;
  638. fmt->field = V4L2_FIELD_NONE;
  639. fmt->colorspace = ov7740_formats[index].colorspace;
  640. ov7740->format = *fmt;
  641. return 0;
  642. }
  643. static int ov7740_set_fmt(struct v4l2_subdev *sd,
  644. struct v4l2_subdev_pad_config *cfg,
  645. struct v4l2_subdev_format *format)
  646. {
  647. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  648. const struct ov7740_pixfmt *ovfmt;
  649. const struct ov7740_framesize *fsize;
  650. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  651. struct v4l2_mbus_framefmt *mbus_fmt;
  652. #endif
  653. int ret;
  654. mutex_lock(&ov7740->mutex);
  655. if (format->pad) {
  656. ret = -EINVAL;
  657. goto error;
  658. }
  659. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  660. ret = ov7740_try_fmt_internal(sd, &format->format, NULL, NULL);
  661. if (ret)
  662. goto error;
  663. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  664. mbus_fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
  665. *mbus_fmt = format->format;
  666. mutex_unlock(&ov7740->mutex);
  667. return 0;
  668. #else
  669. ret = -ENOTTY;
  670. goto error;
  671. #endif
  672. }
  673. ret = ov7740_try_fmt_internal(sd, &format->format, &ovfmt, &fsize);
  674. if (ret)
  675. goto error;
  676. ov7740->fmt = ovfmt;
  677. ov7740->frmsize = fsize;
  678. mutex_unlock(&ov7740->mutex);
  679. return 0;
  680. error:
  681. mutex_unlock(&ov7740->mutex);
  682. return ret;
  683. }
  684. static int ov7740_get_fmt(struct v4l2_subdev *sd,
  685. struct v4l2_subdev_pad_config *cfg,
  686. struct v4l2_subdev_format *format)
  687. {
  688. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  689. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  690. struct v4l2_mbus_framefmt *mbus_fmt;
  691. #endif
  692. int ret = 0;
  693. mutex_lock(&ov7740->mutex);
  694. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  695. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  696. mbus_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
  697. format->format = *mbus_fmt;
  698. ret = 0;
  699. #else
  700. ret = -ENOTTY;
  701. #endif
  702. } else {
  703. format->format = ov7740->format;
  704. }
  705. mutex_unlock(&ov7740->mutex);
  706. return ret;
  707. }
  708. static const struct v4l2_subdev_pad_ops ov7740_subdev_pad_ops = {
  709. .enum_frame_interval = ov7740_enum_frame_interval,
  710. .enum_frame_size = ov7740_enum_frame_size,
  711. .enum_mbus_code = ov7740_enum_mbus_code,
  712. .get_fmt = ov7740_get_fmt,
  713. .set_fmt = ov7740_set_fmt,
  714. };
  715. static const struct v4l2_subdev_ops ov7740_subdev_ops = {
  716. .core = &ov7740_subdev_core_ops,
  717. .video = &ov7740_subdev_video_ops,
  718. .pad = &ov7740_subdev_pad_ops,
  719. };
  720. static void ov7740_get_default_format(struct v4l2_subdev *sd,
  721. struct v4l2_mbus_framefmt *format)
  722. {
  723. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  724. format->width = ov7740->frmsize->width;
  725. format->height = ov7740->frmsize->height;
  726. format->colorspace = ov7740->fmt->colorspace;
  727. format->code = ov7740->fmt->mbus_code;
  728. format->field = V4L2_FIELD_NONE;
  729. }
  730. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  731. static int ov7740_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  732. {
  733. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  734. struct v4l2_mbus_framefmt *format =
  735. v4l2_subdev_get_try_format(sd, fh->pad, 0);
  736. mutex_lock(&ov7740->mutex);
  737. ov7740_get_default_format(sd, format);
  738. mutex_unlock(&ov7740->mutex);
  739. return 0;
  740. }
  741. static const struct v4l2_subdev_internal_ops ov7740_subdev_internal_ops = {
  742. .open = ov7740_open,
  743. };
  744. #endif
  745. static int ov7740_probe_dt(struct i2c_client *client,
  746. struct ov7740 *ov7740)
  747. {
  748. ov7740->resetb_gpio = devm_gpiod_get_optional(&client->dev, "reset",
  749. GPIOD_OUT_HIGH);
  750. if (IS_ERR(ov7740->resetb_gpio)) {
  751. dev_info(&client->dev, "can't get %s GPIO\n", "reset");
  752. return PTR_ERR(ov7740->resetb_gpio);
  753. }
  754. ov7740->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "powerdown",
  755. GPIOD_OUT_LOW);
  756. if (IS_ERR(ov7740->pwdn_gpio)) {
  757. dev_info(&client->dev, "can't get %s GPIO\n", "powerdown");
  758. return PTR_ERR(ov7740->pwdn_gpio);
  759. }
  760. return 0;
  761. }
  762. static int ov7740_detect(struct ov7740 *ov7740)
  763. {
  764. struct regmap *regmap = ov7740->regmap;
  765. unsigned int midh, midl, pidh, pidl;
  766. int ret;
  767. ret = regmap_read(regmap, REG_MIDH, &midh);
  768. if (ret)
  769. return ret;
  770. if (midh != 0x7f)
  771. return -ENODEV;
  772. ret = regmap_read(regmap, REG_MIDL, &midl);
  773. if (ret)
  774. return ret;
  775. if (midl != 0xa2)
  776. return -ENODEV;
  777. ret = regmap_read(regmap, REG_PIDH, &pidh);
  778. if (ret)
  779. return ret;
  780. if (pidh != 0x77)
  781. return -ENODEV;
  782. ret = regmap_read(regmap, REG_PIDL, &pidl);
  783. if (ret)
  784. return ret;
  785. if ((pidl != 0x40) && (pidl != 0x41) && (pidl != 0x42))
  786. return -ENODEV;
  787. return 0;
  788. }
  789. static int ov7740_init_controls(struct ov7740 *ov7740)
  790. {
  791. struct i2c_client *client = v4l2_get_subdevdata(&ov7740->subdev);
  792. struct v4l2_ctrl_handler *ctrl_hdlr = &ov7740->ctrl_handler;
  793. int ret;
  794. ret = v4l2_ctrl_handler_init(ctrl_hdlr, 12);
  795. if (ret < 0)
  796. return ret;
  797. ctrl_hdlr->lock = &ov7740->mutex;
  798. ov7740->auto_wb = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  799. V4L2_CID_AUTO_WHITE_BALANCE,
  800. 0, 1, 1, 1);
  801. ov7740->blue_balance = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  802. V4L2_CID_BLUE_BALANCE,
  803. 0, 0xff, 1, 0x80);
  804. ov7740->red_balance = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  805. V4L2_CID_RED_BALANCE,
  806. 0, 0xff, 1, 0x80);
  807. ov7740->brightness = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  808. V4L2_CID_BRIGHTNESS,
  809. -255, 255, 1, 0);
  810. ov7740->contrast = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  811. V4L2_CID_CONTRAST,
  812. 0, 127, 1, 0x20);
  813. ov7740->saturation = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  814. V4L2_CID_SATURATION, 0, 256, 1, 0x80);
  815. ov7740->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  816. V4L2_CID_HFLIP, 0, 1, 1, 0);
  817. ov7740->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  818. V4L2_CID_VFLIP, 0, 1, 1, 0);
  819. ov7740->gain = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  820. V4L2_CID_GAIN, 0, 1023, 1, 500);
  821. if (ov7740->gain)
  822. ov7740->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
  823. ov7740->auto_gain = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  824. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  825. ov7740->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  826. V4L2_CID_EXPOSURE, 0, 65535, 1, 500);
  827. if (ov7740->exposure)
  828. ov7740->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
  829. ov7740->auto_exposure = v4l2_ctrl_new_std_menu(ctrl_hdlr,
  830. &ov7740_ctrl_ops,
  831. V4L2_CID_EXPOSURE_AUTO,
  832. V4L2_EXPOSURE_MANUAL, 0,
  833. V4L2_EXPOSURE_AUTO);
  834. v4l2_ctrl_auto_cluster(3, &ov7740->auto_wb, 0, false);
  835. v4l2_ctrl_auto_cluster(2, &ov7740->auto_gain, 0, true);
  836. v4l2_ctrl_auto_cluster(2, &ov7740->auto_exposure,
  837. V4L2_EXPOSURE_MANUAL, false);
  838. v4l2_ctrl_cluster(2, &ov7740->hflip);
  839. if (ctrl_hdlr->error) {
  840. ret = ctrl_hdlr->error;
  841. dev_err(&client->dev, "controls initialisation failed (%d)\n",
  842. ret);
  843. goto error;
  844. }
  845. ret = v4l2_ctrl_handler_setup(ctrl_hdlr);
  846. if (ret) {
  847. dev_err(&client->dev, "%s control init failed (%d)\n",
  848. __func__, ret);
  849. goto error;
  850. }
  851. ov7740->subdev.ctrl_handler = ctrl_hdlr;
  852. return 0;
  853. error:
  854. v4l2_ctrl_handler_free(ctrl_hdlr);
  855. mutex_destroy(&ov7740->mutex);
  856. return ret;
  857. }
  858. static void ov7740_free_controls(struct ov7740 *ov7740)
  859. {
  860. v4l2_ctrl_handler_free(ov7740->subdev.ctrl_handler);
  861. mutex_destroy(&ov7740->mutex);
  862. }
  863. #define OV7740_MAX_REGISTER 0xff
  864. static const struct regmap_config ov7740_regmap_config = {
  865. .reg_bits = 8,
  866. .val_bits = 8,
  867. .max_register = OV7740_MAX_REGISTER,
  868. };
  869. static int ov7740_probe(struct i2c_client *client,
  870. const struct i2c_device_id *id)
  871. {
  872. struct ov7740 *ov7740;
  873. struct v4l2_subdev *sd;
  874. int ret;
  875. if (!i2c_check_functionality(client->adapter,
  876. I2C_FUNC_SMBUS_BYTE_DATA)) {
  877. dev_err(&client->dev,
  878. "OV7740: I2C-Adapter doesn't support SMBUS\n");
  879. return -EIO;
  880. }
  881. ov7740 = devm_kzalloc(&client->dev, sizeof(*ov7740), GFP_KERNEL);
  882. if (!ov7740)
  883. return -ENOMEM;
  884. ov7740->xvclk = devm_clk_get(&client->dev, "xvclk");
  885. if (IS_ERR(ov7740->xvclk)) {
  886. ret = PTR_ERR(ov7740->xvclk);
  887. dev_err(&client->dev,
  888. "OV7740: fail to get xvclk: %d\n", ret);
  889. return ret;
  890. }
  891. ret = ov7740_probe_dt(client, ov7740);
  892. if (ret)
  893. return ret;
  894. ov7740->regmap = devm_regmap_init_i2c(client, &ov7740_regmap_config);
  895. if (IS_ERR(ov7740->regmap)) {
  896. ret = PTR_ERR(ov7740->regmap);
  897. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  898. ret);
  899. return ret;
  900. }
  901. sd = &ov7740->subdev;
  902. client->flags |= I2C_CLIENT_SCCB;
  903. v4l2_i2c_subdev_init(sd, client, &ov7740_subdev_ops);
  904. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  905. sd->internal_ops = &ov7740_subdev_internal_ops;
  906. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  907. #endif
  908. #if defined(CONFIG_MEDIA_CONTROLLER)
  909. ov7740->pad.flags = MEDIA_PAD_FL_SOURCE;
  910. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  911. ret = media_entity_pads_init(&sd->entity, 1, &ov7740->pad);
  912. if (ret)
  913. return ret;
  914. #endif
  915. ret = ov7740_set_power(ov7740, 1);
  916. if (ret)
  917. return ret;
  918. ret = ov7740_detect(ov7740);
  919. if (ret)
  920. goto error_detect;
  921. mutex_init(&ov7740->mutex);
  922. ret = ov7740_init_controls(ov7740);
  923. if (ret)
  924. goto error_init_controls;
  925. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  926. client->addr << 1, client->adapter->name);
  927. ov7740->fmt = &ov7740_formats[0];
  928. ov7740->frmsize = &ov7740_framesizes[0];
  929. ov7740_get_default_format(sd, &ov7740->format);
  930. ret = v4l2_async_register_subdev(sd);
  931. if (ret)
  932. goto error_async_register;
  933. pm_runtime_set_active(&client->dev);
  934. pm_runtime_enable(&client->dev);
  935. pm_runtime_idle(&client->dev);
  936. return 0;
  937. error_async_register:
  938. v4l2_ctrl_handler_free(ov7740->subdev.ctrl_handler);
  939. error_init_controls:
  940. ov7740_free_controls(ov7740);
  941. error_detect:
  942. ov7740_set_power(ov7740, 0);
  943. media_entity_cleanup(&ov7740->subdev.entity);
  944. return ret;
  945. }
  946. static int ov7740_remove(struct i2c_client *client)
  947. {
  948. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  949. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  950. mutex_destroy(&ov7740->mutex);
  951. v4l2_ctrl_handler_free(ov7740->subdev.ctrl_handler);
  952. media_entity_cleanup(&ov7740->subdev.entity);
  953. v4l2_async_unregister_subdev(sd);
  954. ov7740_free_controls(ov7740);
  955. pm_runtime_get_sync(&client->dev);
  956. pm_runtime_disable(&client->dev);
  957. pm_runtime_set_suspended(&client->dev);
  958. pm_runtime_put_noidle(&client->dev);
  959. ov7740_set_power(ov7740, 0);
  960. return 0;
  961. }
  962. static int __maybe_unused ov7740_runtime_suspend(struct device *dev)
  963. {
  964. struct i2c_client *client = to_i2c_client(dev);
  965. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  966. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  967. ov7740_set_power(ov7740, 0);
  968. return 0;
  969. }
  970. static int __maybe_unused ov7740_runtime_resume(struct device *dev)
  971. {
  972. struct i2c_client *client = to_i2c_client(dev);
  973. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  974. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  975. return ov7740_set_power(ov7740, 1);
  976. }
  977. static const struct i2c_device_id ov7740_id[] = {
  978. { "ov7740", 0 },
  979. { /* sentinel */ }
  980. };
  981. MODULE_DEVICE_TABLE(i2c, ov7740_id);
  982. static const struct dev_pm_ops ov7740_pm_ops = {
  983. SET_RUNTIME_PM_OPS(ov7740_runtime_suspend, ov7740_runtime_resume, NULL)
  984. };
  985. static const struct of_device_id ov7740_of_match[] = {
  986. {.compatible = "ovti,ov7740", },
  987. { /* sentinel */ },
  988. };
  989. MODULE_DEVICE_TABLE(of, ov7740_of_match);
  990. static struct i2c_driver ov7740_i2c_driver = {
  991. .driver = {
  992. .name = "ov7740",
  993. .pm = &ov7740_pm_ops,
  994. .of_match_table = of_match_ptr(ov7740_of_match),
  995. },
  996. .probe = ov7740_probe,
  997. .remove = ov7740_remove,
  998. .id_table = ov7740_id,
  999. };
  1000. module_i2c_driver(ov7740_i2c_driver);
  1001. MODULE_DESCRIPTION("The V4L2 driver for Omnivision 7740 sensor");
  1002. MODULE_AUTHOR("Songjun Wu <songjun.wu@atmel.com>");
  1003. MODULE_LICENSE("GPL v2");