ov7740.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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, don't 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 const 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_get_exp(struct ov7740 *ov7740, struct v4l2_ctrl *ctrl)
  381. {
  382. struct regmap *regmap = ov7740->regmap;
  383. unsigned int value0, value1;
  384. int ret;
  385. if (ctrl->val == V4L2_EXPOSURE_MANUAL)
  386. return 0;
  387. ret = regmap_read(regmap, REG_AEC, &value0);
  388. if (ret)
  389. return ret;
  390. ret = regmap_read(regmap, REG_HAEC, &value1);
  391. if (ret)
  392. return ret;
  393. ov7740->exposure->val = (value1 << 8) | (value0 & 0xff);
  394. return 0;
  395. }
  396. static int ov7740_set_exp(struct regmap *regmap, int value)
  397. {
  398. int ret;
  399. /* Turn off AEC/AGC */
  400. ret = regmap_update_bits(regmap, REG_REG13,
  401. REG13_AEC_EN | REG13_AGC_EN, 0);
  402. if (ret)
  403. return ret;
  404. ret = regmap_write(regmap, REG_AEC, (unsigned char)value);
  405. if (ret)
  406. return ret;
  407. return regmap_write(regmap, REG_HAEC, (unsigned char)(value >> 8));
  408. }
  409. static int ov7740_set_autoexp(struct regmap *regmap,
  410. enum v4l2_exposure_auto_type value)
  411. {
  412. unsigned int reg;
  413. int ret;
  414. ret = regmap_read(regmap, REG_REG13, &reg);
  415. if (!ret) {
  416. if (value == V4L2_EXPOSURE_AUTO)
  417. reg |= (REG13_AEC_EN | REG13_AGC_EN);
  418. else
  419. reg &= ~(REG13_AEC_EN | REG13_AGC_EN);
  420. ret = regmap_write(regmap, REG_REG13, reg);
  421. }
  422. return ret;
  423. }
  424. static int ov7740_get_volatile_ctrl(struct v4l2_ctrl *ctrl)
  425. {
  426. struct ov7740 *ov7740 = container_of(ctrl->handler,
  427. struct ov7740, ctrl_handler);
  428. int ret;
  429. switch (ctrl->id) {
  430. case V4L2_CID_AUTOGAIN:
  431. ret = ov7740_get_gain(ov7740, ctrl);
  432. break;
  433. case V4L2_CID_EXPOSURE_AUTO:
  434. ret = ov7740_get_exp(ov7740, ctrl);
  435. break;
  436. default:
  437. ret = -EINVAL;
  438. break;
  439. }
  440. return ret;
  441. }
  442. static int ov7740_set_ctrl(struct v4l2_ctrl *ctrl)
  443. {
  444. struct ov7740 *ov7740 = container_of(ctrl->handler,
  445. struct ov7740, ctrl_handler);
  446. struct i2c_client *client = v4l2_get_subdevdata(&ov7740->subdev);
  447. struct regmap *regmap = ov7740->regmap;
  448. int ret;
  449. u8 val;
  450. if (!pm_runtime_get_if_in_use(&client->dev))
  451. return 0;
  452. switch (ctrl->id) {
  453. case V4L2_CID_AUTO_WHITE_BALANCE:
  454. ret = ov7740_set_white_balance(ov7740, ctrl->val);
  455. break;
  456. case V4L2_CID_SATURATION:
  457. ret = ov7740_set_saturation(regmap, ctrl->val);
  458. break;
  459. case V4L2_CID_BRIGHTNESS:
  460. ret = ov7740_set_brightness(regmap, ctrl->val);
  461. break;
  462. case V4L2_CID_CONTRAST:
  463. ret = ov7740_set_contrast(regmap, ctrl->val);
  464. break;
  465. case V4L2_CID_VFLIP:
  466. val = ctrl->val ? REG0C_IMG_FLIP : 0x00;
  467. ret = regmap_update_bits(regmap, REG_REG0C,
  468. REG0C_IMG_FLIP, val);
  469. break;
  470. case V4L2_CID_HFLIP:
  471. val = ctrl->val ? REG0C_IMG_MIRROR : 0x00;
  472. ret = regmap_update_bits(regmap, REG_REG0C,
  473. REG0C_IMG_MIRROR, val);
  474. break;
  475. case V4L2_CID_AUTOGAIN:
  476. if (!ctrl->val)
  477. ret = ov7740_set_gain(regmap, ov7740->gain->val);
  478. else
  479. ret = ov7740_set_autogain(regmap, ctrl->val);
  480. break;
  481. case V4L2_CID_EXPOSURE_AUTO:
  482. if (ctrl->val == V4L2_EXPOSURE_MANUAL)
  483. ret = ov7740_set_exp(regmap, ov7740->exposure->val);
  484. else
  485. ret = ov7740_set_autoexp(regmap, ctrl->val);
  486. break;
  487. default:
  488. ret = -EINVAL;
  489. break;
  490. }
  491. pm_runtime_put(&client->dev);
  492. return ret;
  493. }
  494. static const struct v4l2_ctrl_ops ov7740_ctrl_ops = {
  495. .g_volatile_ctrl = ov7740_get_volatile_ctrl,
  496. .s_ctrl = ov7740_set_ctrl,
  497. };
  498. static int ov7740_start_streaming(struct ov7740 *ov7740)
  499. {
  500. int ret;
  501. if (ov7740->fmt) {
  502. ret = regmap_multi_reg_write(ov7740->regmap,
  503. ov7740->fmt->regs,
  504. ov7740->fmt->reg_num);
  505. if (ret)
  506. return ret;
  507. }
  508. if (ov7740->frmsize) {
  509. ret = regmap_multi_reg_write(ov7740->regmap,
  510. ov7740->frmsize->regs,
  511. ov7740->frmsize->reg_num);
  512. if (ret)
  513. return ret;
  514. }
  515. return __v4l2_ctrl_handler_setup(ov7740->subdev.ctrl_handler);
  516. }
  517. static int ov7740_set_stream(struct v4l2_subdev *sd, int enable)
  518. {
  519. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  520. struct i2c_client *client = v4l2_get_subdevdata(sd);
  521. int ret = 0;
  522. mutex_lock(&ov7740->mutex);
  523. if (ov7740->streaming == enable) {
  524. mutex_unlock(&ov7740->mutex);
  525. return 0;
  526. }
  527. if (enable) {
  528. ret = pm_runtime_resume_and_get(&client->dev);
  529. if (ret < 0)
  530. goto err_unlock;
  531. ret = ov7740_start_streaming(ov7740);
  532. if (ret)
  533. goto err_rpm_put;
  534. } else {
  535. pm_runtime_put(&client->dev);
  536. }
  537. ov7740->streaming = enable;
  538. mutex_unlock(&ov7740->mutex);
  539. return ret;
  540. err_rpm_put:
  541. pm_runtime_put(&client->dev);
  542. err_unlock:
  543. mutex_unlock(&ov7740->mutex);
  544. return ret;
  545. }
  546. static int ov7740_g_frame_interval(struct v4l2_subdev *sd,
  547. struct v4l2_subdev_frame_interval *ival)
  548. {
  549. struct v4l2_fract *tpf = &ival->interval;
  550. tpf->numerator = 1;
  551. tpf->denominator = 60;
  552. return 0;
  553. }
  554. static int ov7740_s_frame_interval(struct v4l2_subdev *sd,
  555. struct v4l2_subdev_frame_interval *ival)
  556. {
  557. struct v4l2_fract *tpf = &ival->interval;
  558. tpf->numerator = 1;
  559. tpf->denominator = 60;
  560. return 0;
  561. }
  562. static const struct v4l2_subdev_video_ops ov7740_subdev_video_ops = {
  563. .s_stream = ov7740_set_stream,
  564. .s_frame_interval = ov7740_s_frame_interval,
  565. .g_frame_interval = ov7740_g_frame_interval,
  566. };
  567. static const struct reg_sequence ov7740_format_yuyv[] = {
  568. {0x12, 0x00},
  569. {0x36, 0x3f},
  570. {0x80, 0x7f},
  571. {0x83, 0x01},
  572. };
  573. static const struct reg_sequence ov7740_format_bggr8[] = {
  574. {0x36, 0x2f},
  575. {0x80, 0x01},
  576. {0x83, 0x04},
  577. };
  578. static const struct ov7740_pixfmt ov7740_formats[] = {
  579. {
  580. .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
  581. .colorspace = V4L2_COLORSPACE_SRGB,
  582. .regs = ov7740_format_yuyv,
  583. .reg_num = ARRAY_SIZE(ov7740_format_yuyv),
  584. },
  585. {
  586. .mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
  587. .colorspace = V4L2_COLORSPACE_SRGB,
  588. .regs = ov7740_format_bggr8,
  589. .reg_num = ARRAY_SIZE(ov7740_format_bggr8),
  590. }
  591. };
  592. #define N_OV7740_FMTS ARRAY_SIZE(ov7740_formats)
  593. static int ov7740_enum_mbus_code(struct v4l2_subdev *sd,
  594. struct v4l2_subdev_state *sd_state,
  595. struct v4l2_subdev_mbus_code_enum *code)
  596. {
  597. if (code->pad || code->index >= N_OV7740_FMTS)
  598. return -EINVAL;
  599. code->code = ov7740_formats[code->index].mbus_code;
  600. return 0;
  601. }
  602. static int ov7740_enum_frame_interval(struct v4l2_subdev *sd,
  603. struct v4l2_subdev_state *sd_state,
  604. struct v4l2_subdev_frame_interval_enum *fie)
  605. {
  606. if (fie->pad)
  607. return -EINVAL;
  608. if (fie->index >= 1)
  609. return -EINVAL;
  610. if ((fie->width != VGA_WIDTH) || (fie->height != VGA_HEIGHT))
  611. return -EINVAL;
  612. fie->interval.numerator = 1;
  613. fie->interval.denominator = 60;
  614. return 0;
  615. }
  616. static int ov7740_enum_frame_size(struct v4l2_subdev *sd,
  617. struct v4l2_subdev_state *sd_state,
  618. struct v4l2_subdev_frame_size_enum *fse)
  619. {
  620. if (fse->pad)
  621. return -EINVAL;
  622. if (fse->index > 0)
  623. return -EINVAL;
  624. fse->min_width = fse->max_width = VGA_WIDTH;
  625. fse->min_height = fse->max_height = VGA_HEIGHT;
  626. return 0;
  627. }
  628. static int ov7740_try_fmt_internal(struct v4l2_subdev *sd,
  629. struct v4l2_mbus_framefmt *fmt,
  630. const struct ov7740_pixfmt **ret_fmt,
  631. const struct ov7740_framesize **ret_frmsize)
  632. {
  633. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  634. const struct ov7740_framesize *fsize = &ov7740_framesizes[0];
  635. int index, i;
  636. for (index = 0; index < N_OV7740_FMTS; index++) {
  637. if (ov7740_formats[index].mbus_code == fmt->code)
  638. break;
  639. }
  640. if (index >= N_OV7740_FMTS) {
  641. /* default to first format */
  642. index = 0;
  643. fmt->code = ov7740_formats[0].mbus_code;
  644. }
  645. if (ret_fmt != NULL)
  646. *ret_fmt = ov7740_formats + index;
  647. for (i = 0; i < ARRAY_SIZE(ov7740_framesizes); i++) {
  648. if ((fsize->width >= fmt->width) &&
  649. (fsize->height >= fmt->height)) {
  650. fmt->width = fsize->width;
  651. fmt->height = fsize->height;
  652. break;
  653. }
  654. fsize++;
  655. }
  656. if (i >= ARRAY_SIZE(ov7740_framesizes)) {
  657. fsize = &ov7740_framesizes[0];
  658. fmt->width = fsize->width;
  659. fmt->height = fsize->height;
  660. }
  661. if (ret_frmsize != NULL)
  662. *ret_frmsize = fsize;
  663. fmt->field = V4L2_FIELD_NONE;
  664. fmt->colorspace = ov7740_formats[index].colorspace;
  665. ov7740->format = *fmt;
  666. return 0;
  667. }
  668. static int ov7740_set_fmt(struct v4l2_subdev *sd,
  669. struct v4l2_subdev_state *sd_state,
  670. struct v4l2_subdev_format *format)
  671. {
  672. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  673. const struct ov7740_pixfmt *ovfmt;
  674. const struct ov7740_framesize *fsize;
  675. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  676. struct v4l2_mbus_framefmt *mbus_fmt;
  677. #endif
  678. int ret;
  679. mutex_lock(&ov7740->mutex);
  680. if (format->pad) {
  681. ret = -EINVAL;
  682. goto error;
  683. }
  684. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  685. ret = ov7740_try_fmt_internal(sd, &format->format, NULL, NULL);
  686. if (ret)
  687. goto error;
  688. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  689. mbus_fmt = v4l2_subdev_get_try_format(sd, sd_state,
  690. format->pad);
  691. *mbus_fmt = format->format;
  692. #endif
  693. mutex_unlock(&ov7740->mutex);
  694. return 0;
  695. }
  696. ret = ov7740_try_fmt_internal(sd, &format->format, &ovfmt, &fsize);
  697. if (ret)
  698. goto error;
  699. ov7740->fmt = ovfmt;
  700. ov7740->frmsize = fsize;
  701. mutex_unlock(&ov7740->mutex);
  702. return 0;
  703. error:
  704. mutex_unlock(&ov7740->mutex);
  705. return ret;
  706. }
  707. static int ov7740_get_fmt(struct v4l2_subdev *sd,
  708. struct v4l2_subdev_state *sd_state,
  709. struct v4l2_subdev_format *format)
  710. {
  711. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  712. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  713. struct v4l2_mbus_framefmt *mbus_fmt;
  714. #endif
  715. int ret = 0;
  716. mutex_lock(&ov7740->mutex);
  717. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  718. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  719. mbus_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
  720. format->format = *mbus_fmt;
  721. ret = 0;
  722. #else
  723. ret = -EINVAL;
  724. #endif
  725. } else {
  726. format->format = ov7740->format;
  727. }
  728. mutex_unlock(&ov7740->mutex);
  729. return ret;
  730. }
  731. static const struct v4l2_subdev_pad_ops ov7740_subdev_pad_ops = {
  732. .enum_frame_interval = ov7740_enum_frame_interval,
  733. .enum_frame_size = ov7740_enum_frame_size,
  734. .enum_mbus_code = ov7740_enum_mbus_code,
  735. .get_fmt = ov7740_get_fmt,
  736. .set_fmt = ov7740_set_fmt,
  737. };
  738. static const struct v4l2_subdev_ops ov7740_subdev_ops = {
  739. .core = &ov7740_subdev_core_ops,
  740. .video = &ov7740_subdev_video_ops,
  741. .pad = &ov7740_subdev_pad_ops,
  742. };
  743. static void ov7740_get_default_format(struct v4l2_subdev *sd,
  744. struct v4l2_mbus_framefmt *format)
  745. {
  746. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  747. format->width = ov7740->frmsize->width;
  748. format->height = ov7740->frmsize->height;
  749. format->colorspace = ov7740->fmt->colorspace;
  750. format->code = ov7740->fmt->mbus_code;
  751. format->field = V4L2_FIELD_NONE;
  752. }
  753. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  754. static int ov7740_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  755. {
  756. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  757. struct v4l2_mbus_framefmt *format =
  758. v4l2_subdev_get_try_format(sd, fh->state, 0);
  759. mutex_lock(&ov7740->mutex);
  760. ov7740_get_default_format(sd, format);
  761. mutex_unlock(&ov7740->mutex);
  762. return 0;
  763. }
  764. static const struct v4l2_subdev_internal_ops ov7740_subdev_internal_ops = {
  765. .open = ov7740_open,
  766. };
  767. #endif
  768. static int ov7740_probe_dt(struct i2c_client *client,
  769. struct ov7740 *ov7740)
  770. {
  771. ov7740->resetb_gpio = devm_gpiod_get_optional(&client->dev, "reset",
  772. GPIOD_OUT_HIGH);
  773. if (IS_ERR(ov7740->resetb_gpio)) {
  774. dev_info(&client->dev, "can't get %s GPIO\n", "reset");
  775. return PTR_ERR(ov7740->resetb_gpio);
  776. }
  777. ov7740->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "powerdown",
  778. GPIOD_OUT_LOW);
  779. if (IS_ERR(ov7740->pwdn_gpio)) {
  780. dev_info(&client->dev, "can't get %s GPIO\n", "powerdown");
  781. return PTR_ERR(ov7740->pwdn_gpio);
  782. }
  783. return 0;
  784. }
  785. static int ov7740_detect(struct ov7740 *ov7740)
  786. {
  787. struct regmap *regmap = ov7740->regmap;
  788. unsigned int midh, midl, pidh, pidl;
  789. int ret;
  790. ret = regmap_read(regmap, REG_MIDH, &midh);
  791. if (ret)
  792. return ret;
  793. if (midh != 0x7f)
  794. return -ENODEV;
  795. ret = regmap_read(regmap, REG_MIDL, &midl);
  796. if (ret)
  797. return ret;
  798. if (midl != 0xa2)
  799. return -ENODEV;
  800. ret = regmap_read(regmap, REG_PIDH, &pidh);
  801. if (ret)
  802. return ret;
  803. if (pidh != 0x77)
  804. return -ENODEV;
  805. ret = regmap_read(regmap, REG_PIDL, &pidl);
  806. if (ret)
  807. return ret;
  808. if ((pidl != 0x40) && (pidl != 0x41) && (pidl != 0x42))
  809. return -ENODEV;
  810. return 0;
  811. }
  812. static int ov7740_init_controls(struct ov7740 *ov7740)
  813. {
  814. struct i2c_client *client = v4l2_get_subdevdata(&ov7740->subdev);
  815. struct v4l2_ctrl_handler *ctrl_hdlr = &ov7740->ctrl_handler;
  816. int ret;
  817. ret = v4l2_ctrl_handler_init(ctrl_hdlr, 12);
  818. if (ret < 0)
  819. return ret;
  820. ctrl_hdlr->lock = &ov7740->mutex;
  821. ov7740->auto_wb = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  822. V4L2_CID_AUTO_WHITE_BALANCE,
  823. 0, 1, 1, 1);
  824. ov7740->blue_balance = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  825. V4L2_CID_BLUE_BALANCE,
  826. 0, 0xff, 1, 0x80);
  827. ov7740->red_balance = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  828. V4L2_CID_RED_BALANCE,
  829. 0, 0xff, 1, 0x80);
  830. ov7740->brightness = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  831. V4L2_CID_BRIGHTNESS,
  832. -255, 255, 1, 0);
  833. ov7740->contrast = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  834. V4L2_CID_CONTRAST,
  835. 0, 127, 1, 0x20);
  836. ov7740->saturation = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  837. V4L2_CID_SATURATION, 0, 256, 1, 0x80);
  838. ov7740->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  839. V4L2_CID_HFLIP, 0, 1, 1, 0);
  840. ov7740->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  841. V4L2_CID_VFLIP, 0, 1, 1, 0);
  842. ov7740->gain = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  843. V4L2_CID_GAIN, 0, 1023, 1, 500);
  844. ov7740->auto_gain = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  845. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  846. ov7740->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
  847. V4L2_CID_EXPOSURE, 0, 65535, 1, 500);
  848. ov7740->auto_exposure = v4l2_ctrl_new_std_menu(ctrl_hdlr,
  849. &ov7740_ctrl_ops,
  850. V4L2_CID_EXPOSURE_AUTO,
  851. V4L2_EXPOSURE_MANUAL, 0,
  852. V4L2_EXPOSURE_AUTO);
  853. v4l2_ctrl_auto_cluster(3, &ov7740->auto_wb, 0, false);
  854. v4l2_ctrl_auto_cluster(2, &ov7740->auto_gain, 0, true);
  855. v4l2_ctrl_auto_cluster(2, &ov7740->auto_exposure,
  856. V4L2_EXPOSURE_MANUAL, true);
  857. if (ctrl_hdlr->error) {
  858. ret = ctrl_hdlr->error;
  859. dev_err(&client->dev, "controls initialisation failed (%d)\n",
  860. ret);
  861. goto error;
  862. }
  863. ret = v4l2_ctrl_handler_setup(ctrl_hdlr);
  864. if (ret) {
  865. dev_err(&client->dev, "%s control init failed (%d)\n",
  866. __func__, ret);
  867. goto error;
  868. }
  869. ov7740->subdev.ctrl_handler = ctrl_hdlr;
  870. return 0;
  871. error:
  872. v4l2_ctrl_handler_free(ctrl_hdlr);
  873. mutex_destroy(&ov7740->mutex);
  874. return ret;
  875. }
  876. static void ov7740_free_controls(struct ov7740 *ov7740)
  877. {
  878. v4l2_ctrl_handler_free(ov7740->subdev.ctrl_handler);
  879. mutex_destroy(&ov7740->mutex);
  880. }
  881. #define OV7740_MAX_REGISTER 0xff
  882. static const struct regmap_config ov7740_regmap_config = {
  883. .reg_bits = 8,
  884. .val_bits = 8,
  885. .max_register = OV7740_MAX_REGISTER,
  886. };
  887. static int ov7740_probe(struct i2c_client *client)
  888. {
  889. struct ov7740 *ov7740;
  890. struct v4l2_subdev *sd;
  891. int ret;
  892. ov7740 = devm_kzalloc(&client->dev, sizeof(*ov7740), GFP_KERNEL);
  893. if (!ov7740)
  894. return -ENOMEM;
  895. ov7740->xvclk = devm_clk_get(&client->dev, "xvclk");
  896. if (IS_ERR(ov7740->xvclk)) {
  897. ret = PTR_ERR(ov7740->xvclk);
  898. dev_err(&client->dev,
  899. "OV7740: fail to get xvclk: %d\n", ret);
  900. return ret;
  901. }
  902. ret = ov7740_probe_dt(client, ov7740);
  903. if (ret)
  904. return ret;
  905. ov7740->regmap = devm_regmap_init_sccb(client, &ov7740_regmap_config);
  906. if (IS_ERR(ov7740->regmap)) {
  907. ret = PTR_ERR(ov7740->regmap);
  908. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  909. ret);
  910. return ret;
  911. }
  912. sd = &ov7740->subdev;
  913. v4l2_i2c_subdev_init(sd, client, &ov7740_subdev_ops);
  914. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  915. sd->internal_ops = &ov7740_subdev_internal_ops;
  916. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  917. #endif
  918. #if defined(CONFIG_MEDIA_CONTROLLER)
  919. ov7740->pad.flags = MEDIA_PAD_FL_SOURCE;
  920. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  921. ret = media_entity_pads_init(&sd->entity, 1, &ov7740->pad);
  922. if (ret)
  923. return ret;
  924. #endif
  925. ret = ov7740_set_power(ov7740, 1);
  926. if (ret)
  927. return ret;
  928. pm_runtime_set_active(&client->dev);
  929. pm_runtime_enable(&client->dev);
  930. ret = ov7740_detect(ov7740);
  931. if (ret)
  932. goto error_detect;
  933. mutex_init(&ov7740->mutex);
  934. ret = ov7740_init_controls(ov7740);
  935. if (ret)
  936. goto error_init_controls;
  937. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  938. client->addr << 1, client->adapter->name);
  939. ov7740->fmt = &ov7740_formats[0];
  940. ov7740->frmsize = &ov7740_framesizes[0];
  941. ov7740_get_default_format(sd, &ov7740->format);
  942. ret = v4l2_async_register_subdev(sd);
  943. if (ret)
  944. goto error_async_register;
  945. pm_runtime_idle(&client->dev);
  946. return 0;
  947. error_async_register:
  948. v4l2_ctrl_handler_free(ov7740->subdev.ctrl_handler);
  949. error_init_controls:
  950. ov7740_free_controls(ov7740);
  951. error_detect:
  952. pm_runtime_disable(&client->dev);
  953. pm_runtime_set_suspended(&client->dev);
  954. ov7740_set_power(ov7740, 0);
  955. media_entity_cleanup(&ov7740->subdev.entity);
  956. return ret;
  957. }
  958. static void ov7740_remove(struct i2c_client *client)
  959. {
  960. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  961. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  962. mutex_destroy(&ov7740->mutex);
  963. v4l2_ctrl_handler_free(ov7740->subdev.ctrl_handler);
  964. media_entity_cleanup(&ov7740->subdev.entity);
  965. v4l2_async_unregister_subdev(sd);
  966. ov7740_free_controls(ov7740);
  967. pm_runtime_get_sync(&client->dev);
  968. pm_runtime_disable(&client->dev);
  969. pm_runtime_set_suspended(&client->dev);
  970. pm_runtime_put_noidle(&client->dev);
  971. ov7740_set_power(ov7740, 0);
  972. }
  973. static int __maybe_unused ov7740_runtime_suspend(struct device *dev)
  974. {
  975. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  976. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  977. ov7740_set_power(ov7740, 0);
  978. return 0;
  979. }
  980. static int __maybe_unused ov7740_runtime_resume(struct device *dev)
  981. {
  982. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  983. struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
  984. return ov7740_set_power(ov7740, 1);
  985. }
  986. static const struct i2c_device_id ov7740_id[] = {
  987. { "ov7740", 0 },
  988. { /* sentinel */ }
  989. };
  990. MODULE_DEVICE_TABLE(i2c, ov7740_id);
  991. static const struct dev_pm_ops ov7740_pm_ops = {
  992. SET_RUNTIME_PM_OPS(ov7740_runtime_suspend, ov7740_runtime_resume, NULL)
  993. };
  994. static const struct of_device_id ov7740_of_match[] = {
  995. {.compatible = "ovti,ov7740", },
  996. { /* sentinel */ },
  997. };
  998. MODULE_DEVICE_TABLE(of, ov7740_of_match);
  999. static struct i2c_driver ov7740_i2c_driver = {
  1000. .driver = {
  1001. .name = "ov7740",
  1002. .pm = &ov7740_pm_ops,
  1003. .of_match_table = of_match_ptr(ov7740_of_match),
  1004. },
  1005. .probe_new = ov7740_probe,
  1006. .remove = ov7740_remove,
  1007. .id_table = ov7740_id,
  1008. };
  1009. module_i2c_driver(ov7740_i2c_driver);
  1010. MODULE_DESCRIPTION("The V4L2 driver for Omnivision 7740 sensor");
  1011. MODULE_AUTHOR("Songjun Wu <[email protected]>");
  1012. MODULE_LICENSE("GPL v2");