ov7740.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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))
  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 (i >= ARRAY_SIZE(ov7740_framesizes)) {
  637. fsize = &ov7740_framesizes[0];
  638. fmt->width = fsize->width;
  639. fmt->height = fsize->height;
  640. }
  641. if (ret_frmsize != NULL)
  642. *ret_frmsize = fsize;
  643. fmt->field = V4L2_FIELD_NONE;
  644. fmt->colorspace = ov7740_formats[index].colorspace;
  645. ov7740->format = *fmt;
  646. return 0;
  647. }
  648. static int ov7740_set_fmt(struct v4l2_subdev *sd,
  649. struct v4l2_subdev_pad_config *cfg,
  650. struct v4l2_subdev_format *format)
  651. {
  652. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  653. const struct ov7740_pixfmt *ovfmt;
  654. const struct ov7740_framesize *fsize;
  655. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  656. struct v4l2_mbus_framefmt *mbus_fmt;
  657. #endif
  658. int ret;
  659. mutex_lock(&ov7740->mutex);
  660. if (format->pad) {
  661. ret = -EINVAL;
  662. goto error;
  663. }
  664. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  665. ret = ov7740_try_fmt_internal(sd, &format->format, NULL, NULL);
  666. if (ret)
  667. goto error;
  668. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  669. mbus_fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
  670. *mbus_fmt = format->format;
  671. mutex_unlock(&ov7740->mutex);
  672. return 0;
  673. #else
  674. ret = -ENOTTY;
  675. goto error;
  676. #endif
  677. }
  678. ret = ov7740_try_fmt_internal(sd, &format->format, &ovfmt, &fsize);
  679. if (ret)
  680. goto error;
  681. ov7740->fmt = ovfmt;
  682. ov7740->frmsize = fsize;
  683. mutex_unlock(&ov7740->mutex);
  684. return 0;
  685. error:
  686. mutex_unlock(&ov7740->mutex);
  687. return ret;
  688. }
  689. static int ov7740_get_fmt(struct v4l2_subdev *sd,
  690. struct v4l2_subdev_pad_config *cfg,
  691. struct v4l2_subdev_format *format)
  692. {
  693. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  694. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  695. struct v4l2_mbus_framefmt *mbus_fmt;
  696. #endif
  697. int ret = 0;
  698. mutex_lock(&ov7740->mutex);
  699. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  700. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  701. mbus_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
  702. format->format = *mbus_fmt;
  703. ret = 0;
  704. #else
  705. ret = -ENOTTY;
  706. #endif
  707. } else {
  708. format->format = ov7740->format;
  709. }
  710. mutex_unlock(&ov7740->mutex);
  711. return ret;
  712. }
  713. static const struct v4l2_subdev_pad_ops ov7740_subdev_pad_ops = {
  714. .enum_frame_interval = ov7740_enum_frame_interval,
  715. .enum_frame_size = ov7740_enum_frame_size,
  716. .enum_mbus_code = ov7740_enum_mbus_code,
  717. .get_fmt = ov7740_get_fmt,
  718. .set_fmt = ov7740_set_fmt,
  719. };
  720. static const struct v4l2_subdev_ops ov7740_subdev_ops = {
  721. .core = &ov7740_subdev_core_ops,
  722. .video = &ov7740_subdev_video_ops,
  723. .pad = &ov7740_subdev_pad_ops,
  724. };
  725. static void ov7740_get_default_format(struct v4l2_subdev *sd,
  726. struct v4l2_mbus_framefmt *format)
  727. {
  728. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  729. format->width = ov7740->frmsize->width;
  730. format->height = ov7740->frmsize->height;
  731. format->colorspace = ov7740->fmt->colorspace;
  732. format->code = ov7740->fmt->mbus_code;
  733. format->field = V4L2_FIELD_NONE;
  734. }
  735. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  736. static int ov7740_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  737. {
  738. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  739. struct v4l2_mbus_framefmt *format =
  740. v4l2_subdev_get_try_format(sd, fh->pad, 0);
  741. mutex_lock(&ov7740->mutex);
  742. ov7740_get_default_format(sd, format);
  743. mutex_unlock(&ov7740->mutex);
  744. return 0;
  745. }
  746. static const struct v4l2_subdev_internal_ops ov7740_subdev_internal_ops = {
  747. .open = ov7740_open,
  748. };
  749. #endif
  750. static int ov7740_probe_dt(struct i2c_client *client,
  751. struct ov7740 *ov7740)
  752. {
  753. ov7740->resetb_gpio = devm_gpiod_get_optional(&client->dev, "reset",
  754. GPIOD_OUT_HIGH);
  755. if (IS_ERR(ov7740->resetb_gpio)) {
  756. dev_info(&client->dev, "can't get %s GPIO\n", "reset");
  757. return PTR_ERR(ov7740->resetb_gpio);
  758. }
  759. ov7740->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "powerdown",
  760. GPIOD_OUT_LOW);
  761. if (IS_ERR(ov7740->pwdn_gpio)) {
  762. dev_info(&client->dev, "can't get %s GPIO\n", "powerdown");
  763. return PTR_ERR(ov7740->pwdn_gpio);
  764. }
  765. return 0;
  766. }
  767. static int ov7740_detect(struct ov7740 *ov7740)
  768. {
  769. struct regmap *regmap = ov7740->regmap;
  770. unsigned int midh, midl, pidh, pidl;
  771. int ret;
  772. ret = regmap_read(regmap, REG_MIDH, &midh);
  773. if (ret)
  774. return ret;
  775. if (midh != 0x7f)
  776. return -ENODEV;
  777. ret = regmap_read(regmap, REG_MIDL, &midl);
  778. if (ret)
  779. return ret;
  780. if (midl != 0xa2)
  781. return -ENODEV;
  782. ret = regmap_read(regmap, REG_PIDH, &pidh);
  783. if (ret)
  784. return ret;
  785. if (pidh != 0x77)
  786. return -ENODEV;
  787. ret = regmap_read(regmap, REG_PIDL, &pidl);
  788. if (ret)
  789. return ret;
  790. if ((pidl != 0x40) && (pidl != 0x41) && (pidl != 0x42))
  791. return -ENODEV;
  792. return 0;
  793. }
  794. static int ov7740_init_controls(struct ov7740 *ov7740)
  795. {
  796. struct i2c_client *client = v4l2_get_subdevdata(&ov7740->subdev);
  797. struct v4l2_ctrl_handler *ctrl_hdlr = &ov7740->ctrl_handler;
  798. int ret;
  799. ret = v4l2_ctrl_handler_init(ctrl_hdlr, 12);
  800. if (ret < 0)
  801. return ret;
  802. ctrl_hdlr->lock = &ov7740->mutex;
  803. ov7740->auto_wb = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  804. V4L2_CID_AUTO_WHITE_BALANCE,
  805. 0, 1, 1, 1);
  806. ov7740->blue_balance = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  807. V4L2_CID_BLUE_BALANCE,
  808. 0, 0xff, 1, 0x80);
  809. ov7740->red_balance = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  810. V4L2_CID_RED_BALANCE,
  811. 0, 0xff, 1, 0x80);
  812. ov7740->brightness = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  813. V4L2_CID_BRIGHTNESS,
  814. -255, 255, 1, 0);
  815. ov7740->contrast = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  816. V4L2_CID_CONTRAST,
  817. 0, 127, 1, 0x20);
  818. ov7740->saturation = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  819. V4L2_CID_SATURATION, 0, 256, 1, 0x80);
  820. ov7740->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  821. V4L2_CID_HFLIP, 0, 1, 1, 0);
  822. ov7740->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  823. V4L2_CID_VFLIP, 0, 1, 1, 0);
  824. ov7740->gain = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  825. V4L2_CID_GAIN, 0, 1023, 1, 500);
  826. if (ov7740->gain)
  827. ov7740->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
  828. ov7740->auto_gain = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  829. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  830. ov7740->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  831. V4L2_CID_EXPOSURE, 0, 65535, 1, 500);
  832. if (ov7740->exposure)
  833. ov7740->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
  834. ov7740->auto_exposure = v4l2_ctrl_new_std_menu(ctrl_hdlr,
  835. &ov7740_ctrl_ops,
  836. V4L2_CID_EXPOSURE_AUTO,
  837. V4L2_EXPOSURE_MANUAL, 0,
  838. V4L2_EXPOSURE_AUTO);
  839. v4l2_ctrl_auto_cluster(3, &ov7740->auto_wb, 0, false);
  840. v4l2_ctrl_auto_cluster(2, &ov7740->auto_gain, 0, true);
  841. v4l2_ctrl_auto_cluster(2, &ov7740->auto_exposure,
  842. V4L2_EXPOSURE_MANUAL, false);
  843. v4l2_ctrl_cluster(2, &ov7740->hflip);
  844. if (ctrl_hdlr->error) {
  845. ret = ctrl_hdlr->error;
  846. dev_err(&client->dev, "controls initialisation failed (%d)\n",
  847. ret);
  848. goto error;
  849. }
  850. ret = v4l2_ctrl_handler_setup(ctrl_hdlr);
  851. if (ret) {
  852. dev_err(&client->dev, "%s control init failed (%d)\n",
  853. __func__, ret);
  854. goto error;
  855. }
  856. ov7740->subdev.ctrl_handler = ctrl_hdlr;
  857. return 0;
  858. error:
  859. v4l2_ctrl_handler_free(ctrl_hdlr);
  860. mutex_destroy(&ov7740->mutex);
  861. return ret;
  862. }
  863. static void ov7740_free_controls(struct ov7740 *ov7740)
  864. {
  865. v4l2_ctrl_handler_free(ov7740->subdev.ctrl_handler);
  866. mutex_destroy(&ov7740->mutex);
  867. }
  868. #define OV7740_MAX_REGISTER 0xff
  869. static const struct regmap_config ov7740_regmap_config = {
  870. .reg_bits = 8,
  871. .val_bits = 8,
  872. .max_register = OV7740_MAX_REGISTER,
  873. };
  874. static int ov7740_probe(struct i2c_client *client,
  875. const struct i2c_device_id *id)
  876. {
  877. struct ov7740 *ov7740;
  878. struct v4l2_subdev *sd;
  879. int ret;
  880. if (!i2c_check_functionality(client->adapter,
  881. I2C_FUNC_SMBUS_BYTE_DATA)) {
  882. dev_err(&client->dev,
  883. "OV7740: I2C-Adapter doesn't support SMBUS\n");
  884. return -EIO;
  885. }
  886. ov7740 = devm_kzalloc(&client->dev, sizeof(*ov7740), GFP_KERNEL);
  887. if (!ov7740)
  888. return -ENOMEM;
  889. ov7740->xvclk = devm_clk_get(&client->dev, "xvclk");
  890. if (IS_ERR(ov7740->xvclk)) {
  891. ret = PTR_ERR(ov7740->xvclk);
  892. dev_err(&client->dev,
  893. "OV7740: fail to get xvclk: %d\n", ret);
  894. return ret;
  895. }
  896. ret = ov7740_probe_dt(client, ov7740);
  897. if (ret)
  898. return ret;
  899. ov7740->regmap = devm_regmap_init_i2c(client, &ov7740_regmap_config);
  900. if (IS_ERR(ov7740->regmap)) {
  901. ret = PTR_ERR(ov7740->regmap);
  902. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  903. ret);
  904. return ret;
  905. }
  906. sd = &ov7740->subdev;
  907. client->flags |= I2C_CLIENT_SCCB;
  908. v4l2_i2c_subdev_init(sd, client, &ov7740_subdev_ops);
  909. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  910. sd->internal_ops = &ov7740_subdev_internal_ops;
  911. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  912. #endif
  913. #if defined(CONFIG_MEDIA_CONTROLLER)
  914. ov7740->pad.flags = MEDIA_PAD_FL_SOURCE;
  915. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  916. ret = media_entity_pads_init(&sd->entity, 1, &ov7740->pad);
  917. if (ret)
  918. return ret;
  919. #endif
  920. ret = ov7740_set_power(ov7740, 1);
  921. if (ret)
  922. return ret;
  923. pm_runtime_set_active(&client->dev);
  924. pm_runtime_enable(&client->dev);
  925. ret = ov7740_detect(ov7740);
  926. if (ret)
  927. goto error_detect;
  928. mutex_init(&ov7740->mutex);
  929. ret = ov7740_init_controls(ov7740);
  930. if (ret)
  931. goto error_init_controls;
  932. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  933. client->addr << 1, client->adapter->name);
  934. ov7740->fmt = &ov7740_formats[0];
  935. ov7740->frmsize = &ov7740_framesizes[0];
  936. ov7740_get_default_format(sd, &ov7740->format);
  937. ret = v4l2_async_register_subdev(sd);
  938. if (ret)
  939. goto error_async_register;
  940. pm_runtime_idle(&client->dev);
  941. return 0;
  942. error_async_register:
  943. v4l2_ctrl_handler_free(ov7740->subdev.ctrl_handler);
  944. error_init_controls:
  945. ov7740_free_controls(ov7740);
  946. error_detect:
  947. pm_runtime_disable(&client->dev);
  948. pm_runtime_set_suspended(&client->dev);
  949. ov7740_set_power(ov7740, 0);
  950. media_entity_cleanup(&ov7740->subdev.entity);
  951. return ret;
  952. }
  953. static int ov7740_remove(struct i2c_client *client)
  954. {
  955. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  956. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  957. mutex_destroy(&ov7740->mutex);
  958. v4l2_ctrl_handler_free(ov7740->subdev.ctrl_handler);
  959. media_entity_cleanup(&ov7740->subdev.entity);
  960. v4l2_async_unregister_subdev(sd);
  961. ov7740_free_controls(ov7740);
  962. pm_runtime_get_sync(&client->dev);
  963. pm_runtime_disable(&client->dev);
  964. pm_runtime_set_suspended(&client->dev);
  965. pm_runtime_put_noidle(&client->dev);
  966. ov7740_set_power(ov7740, 0);
  967. return 0;
  968. }
  969. static int __maybe_unused ov7740_runtime_suspend(struct device *dev)
  970. {
  971. struct i2c_client *client = to_i2c_client(dev);
  972. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  973. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  974. ov7740_set_power(ov7740, 0);
  975. return 0;
  976. }
  977. static int __maybe_unused ov7740_runtime_resume(struct device *dev)
  978. {
  979. struct i2c_client *client = to_i2c_client(dev);
  980. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  981. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  982. return ov7740_set_power(ov7740, 1);
  983. }
  984. static const struct i2c_device_id ov7740_id[] = {
  985. { "ov7740", 0 },
  986. { /* sentinel */ }
  987. };
  988. MODULE_DEVICE_TABLE(i2c, ov7740_id);
  989. static const struct dev_pm_ops ov7740_pm_ops = {
  990. SET_RUNTIME_PM_OPS(ov7740_runtime_suspend, ov7740_runtime_resume, NULL)
  991. };
  992. static const struct of_device_id ov7740_of_match[] = {
  993. {.compatible = "ovti,ov7740", },
  994. { /* sentinel */ },
  995. };
  996. MODULE_DEVICE_TABLE(of, ov7740_of_match);
  997. static struct i2c_driver ov7740_i2c_driver = {
  998. .driver = {
  999. .name = "ov7740",
  1000. .pm = &ov7740_pm_ops,
  1001. .of_match_table = of_match_ptr(ov7740_of_match),
  1002. },
  1003. .probe = ov7740_probe,
  1004. .remove = ov7740_remove,
  1005. .id_table = ov7740_id,
  1006. };
  1007. module_i2c_driver(ov7740_i2c_driver);
  1008. MODULE_DESCRIPTION("The V4L2 driver for Omnivision 7740 sensor");
  1009. MODULE_AUTHOR("Songjun Wu <songjun.wu@atmel.com>");
  1010. MODULE_LICENSE("GPL v2");