ov2680.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Omnivision OV2680 CMOS Image Sensor driver
  4. *
  5. * Copyright (C) 2018 Linaro Ltd
  6. *
  7. * Based on OV5640 Sensor Driver
  8. * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
  9. * Copyright (C) 2014-2017 Mentor Graphics Inc.
  10. *
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/i2c.h>
  17. #include <linux/init.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/module.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/regmap.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <media/v4l2-cci.h>
  24. #include <media/v4l2-common.h>
  25. #include <media/v4l2-ctrls.h>
  26. #include <media/v4l2-fwnode.h>
  27. #include <media/v4l2-subdev.h>
  28. #define OV2680_CHIP_ID 0x2680
  29. #define OV2680_REG_STREAM_CTRL CCI_REG8(0x0100)
  30. #define OV2680_REG_SOFT_RESET CCI_REG8(0x0103)
  31. #define OV2680_REG_CHIP_ID CCI_REG16(0x300a)
  32. #define OV2680_REG_SC_CMMN_SUB_ID CCI_REG8(0x302a)
  33. #define OV2680_REG_PLL_MULTIPLIER CCI_REG16(0x3081)
  34. #define OV2680_REG_EXPOSURE_PK CCI_REG24(0x3500)
  35. #define OV2680_REG_R_MANUAL CCI_REG8(0x3503)
  36. #define OV2680_REG_GAIN_PK CCI_REG16(0x350a)
  37. #define OV2680_REG_SENSOR_CTRL_0A CCI_REG8(0x370a)
  38. #define OV2680_REG_HORIZONTAL_START CCI_REG16(0x3800)
  39. #define OV2680_REG_VERTICAL_START CCI_REG16(0x3802)
  40. #define OV2680_REG_HORIZONTAL_END CCI_REG16(0x3804)
  41. #define OV2680_REG_VERTICAL_END CCI_REG16(0x3806)
  42. #define OV2680_REG_HORIZONTAL_OUTPUT_SIZE CCI_REG16(0x3808)
  43. #define OV2680_REG_VERTICAL_OUTPUT_SIZE CCI_REG16(0x380a)
  44. #define OV2680_REG_TIMING_HTS CCI_REG16(0x380c)
  45. #define OV2680_REG_TIMING_VTS CCI_REG16(0x380e)
  46. #define OV2680_REG_ISP_X_WIN CCI_REG16(0x3810)
  47. #define OV2680_REG_ISP_Y_WIN CCI_REG16(0x3812)
  48. #define OV2680_REG_X_INC CCI_REG8(0x3814)
  49. #define OV2680_REG_Y_INC CCI_REG8(0x3815)
  50. #define OV2680_REG_FORMAT1 CCI_REG8(0x3820)
  51. #define OV2680_REG_FORMAT2 CCI_REG8(0x3821)
  52. #define OV2680_REG_ISP_CTRL00 CCI_REG8(0x5080)
  53. #define OV2680_REG_X_WIN CCI_REG16(0x5704)
  54. #define OV2680_REG_Y_WIN CCI_REG16(0x5706)
  55. #define OV2680_FRAME_RATE 30
  56. #define OV2680_NATIVE_WIDTH 1616
  57. #define OV2680_NATIVE_HEIGHT 1216
  58. #define OV2680_NATIVE_START_LEFT 0
  59. #define OV2680_NATIVE_START_TOP 0
  60. #define OV2680_ACTIVE_WIDTH 1600
  61. #define OV2680_ACTIVE_HEIGHT 1200
  62. #define OV2680_ACTIVE_START_LEFT 8
  63. #define OV2680_ACTIVE_START_TOP 8
  64. #define OV2680_MIN_CROP_WIDTH 2
  65. #define OV2680_MIN_CROP_HEIGHT 2
  66. #define OV2680_MIN_VBLANK 4
  67. #define OV2680_MAX_VBLANK 0xffff
  68. /* Fixed pre-div of 1/2 */
  69. #define OV2680_PLL_PREDIV0 2
  70. /* Pre-div configurable through reg 0x3080, left at its default of 0x02 : 1/2 */
  71. #define OV2680_PLL_PREDIV 2
  72. /* 66MHz pixel clock: 66MHz / 1704 * 1294 = 30fps */
  73. #define OV2680_PIXELS_PER_LINE 1704
  74. #define OV2680_LINES_PER_FRAME_30FPS 1294
  75. /* Max exposure time is VTS - 8 */
  76. #define OV2680_INTEGRATION_TIME_MARGIN 8
  77. #define OV2680_DEFAULT_WIDTH 800
  78. #define OV2680_DEFAULT_HEIGHT 600
  79. /* For enum_frame_size() full-size + binned-/quarter-size */
  80. #define OV2680_FRAME_SIZES 2
  81. static const char * const ov2680_supply_name[] = {
  82. "DOVDD",
  83. "DVDD",
  84. "AVDD",
  85. };
  86. #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
  87. enum {
  88. OV2680_19_2_MHZ,
  89. OV2680_24_MHZ,
  90. };
  91. static const unsigned long ov2680_xvclk_freqs[] = {
  92. [OV2680_19_2_MHZ] = 19200000,
  93. [OV2680_24_MHZ] = 24000000,
  94. };
  95. static const u8 ov2680_pll_multipliers[] = {
  96. [OV2680_19_2_MHZ] = 69,
  97. [OV2680_24_MHZ] = 55,
  98. };
  99. struct ov2680_ctrls {
  100. struct v4l2_ctrl_handler handler;
  101. struct v4l2_ctrl *exposure;
  102. struct v4l2_ctrl *gain;
  103. struct v4l2_ctrl *hflip;
  104. struct v4l2_ctrl *vflip;
  105. struct v4l2_ctrl *test_pattern;
  106. struct v4l2_ctrl *link_freq;
  107. struct v4l2_ctrl *pixel_rate;
  108. struct v4l2_ctrl *vblank;
  109. struct v4l2_ctrl *hblank;
  110. };
  111. struct ov2680_mode {
  112. struct v4l2_rect crop;
  113. struct v4l2_mbus_framefmt fmt;
  114. struct v4l2_fract frame_interval;
  115. bool binning;
  116. u16 h_start;
  117. u16 v_start;
  118. u16 h_end;
  119. u16 v_end;
  120. u16 h_output_size;
  121. u16 v_output_size;
  122. };
  123. struct ov2680_dev {
  124. struct device *dev;
  125. struct regmap *regmap;
  126. struct v4l2_subdev sd;
  127. struct media_pad pad;
  128. struct clk *xvclk;
  129. u32 xvclk_freq;
  130. u8 pll_mult;
  131. s64 link_freq[1];
  132. u64 pixel_rate;
  133. struct regulator_bulk_data supplies[OV2680_NUM_SUPPLIES];
  134. struct gpio_desc *pwdn_gpio;
  135. struct mutex lock; /* protect members */
  136. bool is_streaming;
  137. struct ov2680_ctrls ctrls;
  138. struct ov2680_mode mode;
  139. };
  140. static const struct v4l2_rect ov2680_default_crop = {
  141. .left = OV2680_ACTIVE_START_LEFT,
  142. .top = OV2680_ACTIVE_START_TOP,
  143. .width = OV2680_ACTIVE_WIDTH,
  144. .height = OV2680_ACTIVE_HEIGHT,
  145. };
  146. static const char * const test_pattern_menu[] = {
  147. "Disabled",
  148. "Color Bars",
  149. "Random Data",
  150. "Square",
  151. "Black Image",
  152. };
  153. static const int ov2680_hv_flip_bayer_order[] = {
  154. MEDIA_BUS_FMT_SBGGR10_1X10,
  155. MEDIA_BUS_FMT_SGRBG10_1X10,
  156. MEDIA_BUS_FMT_SGBRG10_1X10,
  157. MEDIA_BUS_FMT_SRGGB10_1X10,
  158. };
  159. static const struct reg_sequence ov2680_global_setting[] = {
  160. /* MIPI PHY, 0x10 -> 0x1c enable bp_c_hs_en_lat and bp_d_hs_en_lat */
  161. {0x3016, 0x1c},
  162. /* R MANUAL set exposure and gain to manual (hw does not do auto) */
  163. {0x3503, 0x03},
  164. /* Analog control register tweaks */
  165. {0x3603, 0x39}, /* Reset value 0x99 */
  166. {0x3604, 0x24}, /* Reset value 0x74 */
  167. {0x3621, 0x37}, /* Reset value 0x44 */
  168. /* Sensor control register tweaks */
  169. {0x3701, 0x64}, /* Reset value 0x61 */
  170. {0x3705, 0x3c}, /* Reset value 0x21 */
  171. {0x370c, 0x50}, /* Reset value 0x10 */
  172. {0x370d, 0xc0}, /* Reset value 0x00 */
  173. {0x3718, 0x88}, /* Reset value 0x80 */
  174. /* PSRAM tweaks */
  175. {0x3781, 0x80}, /* Reset value 0x00 */
  176. {0x3784, 0x0c}, /* Reset value 0x00, based on OV2680_R1A_AM10.ovt */
  177. {0x3789, 0x60}, /* Reset value 0x50 */
  178. /* BLC CTRL00 0x01 -> 0x81 set avg_weight to 8 */
  179. {0x4000, 0x81},
  180. /* Set black level compensation range to 0 - 3 (default 0 - 11) */
  181. {0x4008, 0x00},
  182. {0x4009, 0x03},
  183. /* VFIFO R2 0x00 -> 0x02 set Frame reset enable */
  184. {0x4602, 0x02},
  185. /* MIPI ctrl CLK PREPARE MIN change from 0x26 (38) -> 0x36 (54) */
  186. {0x481f, 0x36},
  187. /* MIPI ctrl CLK LPX P MIN change from 0x32 (50) -> 0x36 (54) */
  188. {0x4825, 0x36},
  189. /* R ISP CTRL2 0x20 -> 0x30, set sof_sel bit */
  190. {0x5002, 0x30},
  191. /*
  192. * Window CONTROL 0x00 -> 0x01, enable manual window control,
  193. * this is necessary for full size flip and mirror support.
  194. */
  195. {0x5708, 0x01},
  196. /*
  197. * DPC CTRL0 0x14 -> 0x3e, set enable_tail, enable_3x3_cluster
  198. * and enable_general_tail bits based OV2680_R1A_AM10.ovt.
  199. */
  200. {0x5780, 0x3e},
  201. /* DPC MORE CONNECTION CASE THRE 0x0c (12) -> 0x02 (2) */
  202. {0x5788, 0x02},
  203. /* DPC GAIN LIST1 0x0f (15) -> 0x08 (8) */
  204. {0x578e, 0x08},
  205. /* DPC GAIN LIST2 0x3f (63) -> 0x0c (12) */
  206. {0x578f, 0x0c},
  207. /* DPC THRE RATIO 0x04 (4) -> 0x00 (0) */
  208. {0x5792, 0x00},
  209. };
  210. static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
  211. {
  212. return container_of(sd, struct ov2680_dev, sd);
  213. }
  214. static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
  215. {
  216. return &container_of(ctrl->handler, struct ov2680_dev,
  217. ctrls.handler)->sd;
  218. }
  219. static void ov2680_power_up(struct ov2680_dev *sensor)
  220. {
  221. if (!sensor->pwdn_gpio)
  222. return;
  223. gpiod_set_value(sensor->pwdn_gpio, 0);
  224. usleep_range(5000, 10000);
  225. }
  226. static void ov2680_power_down(struct ov2680_dev *sensor)
  227. {
  228. if (!sensor->pwdn_gpio)
  229. return;
  230. gpiod_set_value(sensor->pwdn_gpio, 1);
  231. usleep_range(5000, 10000);
  232. }
  233. static void ov2680_set_bayer_order(struct ov2680_dev *sensor,
  234. struct v4l2_mbus_framefmt *fmt)
  235. {
  236. int hv_flip = 0;
  237. if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
  238. hv_flip += 1;
  239. if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
  240. hv_flip += 2;
  241. fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
  242. }
  243. static struct v4l2_mbus_framefmt *
  244. __ov2680_get_pad_format(struct ov2680_dev *sensor,
  245. struct v4l2_subdev_state *state,
  246. unsigned int pad,
  247. enum v4l2_subdev_format_whence which)
  248. {
  249. if (which == V4L2_SUBDEV_FORMAT_TRY)
  250. return v4l2_subdev_state_get_format(state, pad);
  251. return &sensor->mode.fmt;
  252. }
  253. static struct v4l2_rect *
  254. __ov2680_get_pad_crop(struct ov2680_dev *sensor,
  255. struct v4l2_subdev_state *state,
  256. unsigned int pad,
  257. enum v4l2_subdev_format_whence which)
  258. {
  259. if (which == V4L2_SUBDEV_FORMAT_TRY)
  260. return v4l2_subdev_state_get_crop(state, pad);
  261. return &sensor->mode.crop;
  262. }
  263. static void ov2680_fill_format(struct ov2680_dev *sensor,
  264. struct v4l2_mbus_framefmt *fmt,
  265. unsigned int width, unsigned int height)
  266. {
  267. memset(fmt, 0, sizeof(*fmt));
  268. fmt->width = width;
  269. fmt->height = height;
  270. fmt->field = V4L2_FIELD_NONE;
  271. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  272. ov2680_set_bayer_order(sensor, fmt);
  273. }
  274. static void ov2680_calc_mode(struct ov2680_dev *sensor)
  275. {
  276. int width = sensor->mode.fmt.width;
  277. int height = sensor->mode.fmt.height;
  278. int orig_width = width;
  279. int orig_height = height;
  280. if (width <= (sensor->mode.crop.width / 2) &&
  281. height <= (sensor->mode.crop.height / 2)) {
  282. sensor->mode.binning = true;
  283. width *= 2;
  284. height *= 2;
  285. } else {
  286. sensor->mode.binning = false;
  287. }
  288. sensor->mode.h_start = (sensor->mode.crop.left +
  289. (sensor->mode.crop.width - width) / 2) & ~1;
  290. sensor->mode.v_start = (sensor->mode.crop.top +
  291. (sensor->mode.crop.height - height) / 2) & ~1;
  292. sensor->mode.h_end =
  293. min(sensor->mode.h_start + width - 1, OV2680_NATIVE_WIDTH - 1);
  294. sensor->mode.v_end =
  295. min(sensor->mode.v_start + height - 1, OV2680_NATIVE_HEIGHT - 1);
  296. sensor->mode.h_output_size = orig_width;
  297. sensor->mode.v_output_size = orig_height;
  298. }
  299. static int ov2680_set_mode(struct ov2680_dev *sensor)
  300. {
  301. u8 sensor_ctrl_0a, inc, fmt1, fmt2;
  302. int ret = 0;
  303. if (sensor->mode.binning) {
  304. sensor_ctrl_0a = 0x23;
  305. inc = 0x31;
  306. fmt1 = 0xc2;
  307. fmt2 = 0x01;
  308. } else {
  309. sensor_ctrl_0a = 0x21;
  310. inc = 0x11;
  311. fmt1 = 0xc0;
  312. fmt2 = 0x00;
  313. }
  314. cci_write(sensor->regmap, OV2680_REG_SENSOR_CTRL_0A,
  315. sensor_ctrl_0a, &ret);
  316. cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_START,
  317. sensor->mode.h_start, &ret);
  318. cci_write(sensor->regmap, OV2680_REG_VERTICAL_START,
  319. sensor->mode.v_start, &ret);
  320. cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_END,
  321. sensor->mode.h_end, &ret);
  322. cci_write(sensor->regmap, OV2680_REG_VERTICAL_END,
  323. sensor->mode.v_end, &ret);
  324. cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_OUTPUT_SIZE,
  325. sensor->mode.h_output_size, &ret);
  326. cci_write(sensor->regmap, OV2680_REG_VERTICAL_OUTPUT_SIZE,
  327. sensor->mode.v_output_size, &ret);
  328. cci_write(sensor->regmap, OV2680_REG_TIMING_HTS,
  329. OV2680_PIXELS_PER_LINE, &ret);
  330. /* VTS gets set by the vblank ctrl */
  331. cci_write(sensor->regmap, OV2680_REG_ISP_X_WIN, 0, &ret);
  332. cci_write(sensor->regmap, OV2680_REG_ISP_Y_WIN, 0, &ret);
  333. cci_write(sensor->regmap, OV2680_REG_X_INC, inc, &ret);
  334. cci_write(sensor->regmap, OV2680_REG_Y_INC, inc, &ret);
  335. cci_write(sensor->regmap, OV2680_REG_X_WIN,
  336. sensor->mode.h_output_size, &ret);
  337. cci_write(sensor->regmap, OV2680_REG_Y_WIN,
  338. sensor->mode.v_output_size, &ret);
  339. cci_write(sensor->regmap, OV2680_REG_FORMAT1, fmt1, &ret);
  340. cci_write(sensor->regmap, OV2680_REG_FORMAT2, fmt2, &ret);
  341. return ret;
  342. }
  343. static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
  344. {
  345. int ret;
  346. if (sensor->is_streaming)
  347. return -EBUSY;
  348. ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT1,
  349. BIT(2), val ? BIT(2) : 0, NULL);
  350. if (ret < 0)
  351. return ret;
  352. ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
  353. return 0;
  354. }
  355. static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
  356. {
  357. int ret;
  358. if (sensor->is_streaming)
  359. return -EBUSY;
  360. ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT2,
  361. BIT(2), val ? BIT(2) : 0, NULL);
  362. if (ret < 0)
  363. return ret;
  364. ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
  365. return 0;
  366. }
  367. static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
  368. {
  369. int ret = 0;
  370. if (!value)
  371. return cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
  372. BIT(7), 0, NULL);
  373. cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
  374. 0x03, value - 1, &ret);
  375. cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
  376. BIT(7), BIT(7), &ret);
  377. return ret;
  378. }
  379. static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
  380. {
  381. return cci_write(sensor->regmap, OV2680_REG_GAIN_PK, gain, NULL);
  382. }
  383. static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
  384. {
  385. return cci_write(sensor->regmap, OV2680_REG_EXPOSURE_PK, exp << 4,
  386. NULL);
  387. }
  388. static int ov2680_exposure_update_range(struct ov2680_dev *sensor)
  389. {
  390. int exp_max = sensor->mode.fmt.height + sensor->ctrls.vblank->val -
  391. OV2680_INTEGRATION_TIME_MARGIN;
  392. return __v4l2_ctrl_modify_range(sensor->ctrls.exposure, 0, exp_max,
  393. 1, exp_max);
  394. }
  395. static int ov2680_stream_enable(struct ov2680_dev *sensor)
  396. {
  397. int ret;
  398. ret = cci_write(sensor->regmap, OV2680_REG_PLL_MULTIPLIER,
  399. sensor->pll_mult, NULL);
  400. if (ret < 0)
  401. return ret;
  402. ret = regmap_multi_reg_write(sensor->regmap,
  403. ov2680_global_setting,
  404. ARRAY_SIZE(ov2680_global_setting));
  405. if (ret < 0)
  406. return ret;
  407. ret = ov2680_set_mode(sensor);
  408. if (ret < 0)
  409. return ret;
  410. /* Restore value of all ctrls */
  411. ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
  412. if (ret < 0)
  413. return ret;
  414. return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 1, NULL);
  415. }
  416. static int ov2680_stream_disable(struct ov2680_dev *sensor)
  417. {
  418. return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 0, NULL);
  419. }
  420. static int ov2680_power_off(struct ov2680_dev *sensor)
  421. {
  422. clk_disable_unprepare(sensor->xvclk);
  423. ov2680_power_down(sensor);
  424. regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
  425. return 0;
  426. }
  427. static int ov2680_power_on(struct ov2680_dev *sensor)
  428. {
  429. int ret;
  430. ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
  431. if (ret < 0) {
  432. dev_err(sensor->dev, "failed to enable regulators: %d\n", ret);
  433. return ret;
  434. }
  435. if (!sensor->pwdn_gpio) {
  436. ret = cci_write(sensor->regmap, OV2680_REG_SOFT_RESET, 0x01,
  437. NULL);
  438. if (ret != 0) {
  439. dev_err(sensor->dev, "sensor soft reset failed\n");
  440. goto err_disable_regulators;
  441. }
  442. usleep_range(1000, 2000);
  443. } else {
  444. ov2680_power_down(sensor);
  445. ov2680_power_up(sensor);
  446. }
  447. ret = clk_prepare_enable(sensor->xvclk);
  448. if (ret < 0)
  449. goto err_disable_regulators;
  450. return 0;
  451. err_disable_regulators:
  452. regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
  453. return ret;
  454. }
  455. static int ov2680_get_frame_interval(struct v4l2_subdev *sd,
  456. struct v4l2_subdev_state *sd_state,
  457. struct v4l2_subdev_frame_interval *fi)
  458. {
  459. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  460. /*
  461. * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
  462. * subdev active state API.
  463. */
  464. if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  465. return -EINVAL;
  466. mutex_lock(&sensor->lock);
  467. fi->interval = sensor->mode.frame_interval;
  468. mutex_unlock(&sensor->lock);
  469. return 0;
  470. }
  471. static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
  472. {
  473. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  474. int ret = 0;
  475. mutex_lock(&sensor->lock);
  476. if (sensor->is_streaming == !!enable)
  477. goto unlock;
  478. if (enable) {
  479. ret = pm_runtime_resume_and_get(sensor->sd.dev);
  480. if (ret < 0)
  481. goto unlock;
  482. ret = ov2680_stream_enable(sensor);
  483. if (ret < 0) {
  484. pm_runtime_put(sensor->sd.dev);
  485. goto unlock;
  486. }
  487. } else {
  488. ret = ov2680_stream_disable(sensor);
  489. pm_runtime_put(sensor->sd.dev);
  490. }
  491. sensor->is_streaming = !!enable;
  492. unlock:
  493. mutex_unlock(&sensor->lock);
  494. return ret;
  495. }
  496. static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
  497. struct v4l2_subdev_state *sd_state,
  498. struct v4l2_subdev_mbus_code_enum *code)
  499. {
  500. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  501. if (code->index != 0)
  502. return -EINVAL;
  503. code->code = sensor->mode.fmt.code;
  504. return 0;
  505. }
  506. static int ov2680_get_fmt(struct v4l2_subdev *sd,
  507. struct v4l2_subdev_state *sd_state,
  508. struct v4l2_subdev_format *format)
  509. {
  510. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  511. struct v4l2_mbus_framefmt *fmt;
  512. fmt = __ov2680_get_pad_format(sensor, sd_state, format->pad,
  513. format->which);
  514. mutex_lock(&sensor->lock);
  515. format->format = *fmt;
  516. mutex_unlock(&sensor->lock);
  517. return 0;
  518. }
  519. static int ov2680_set_fmt(struct v4l2_subdev *sd,
  520. struct v4l2_subdev_state *sd_state,
  521. struct v4l2_subdev_format *format)
  522. {
  523. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  524. struct v4l2_mbus_framefmt *try_fmt;
  525. const struct v4l2_rect *crop;
  526. unsigned int width, height;
  527. int def, max, ret = 0;
  528. crop = __ov2680_get_pad_crop(sensor, sd_state, format->pad,
  529. format->which);
  530. /* Limit set_fmt max size to crop width / height */
  531. width = clamp_val(ALIGN(format->format.width, 2),
  532. OV2680_MIN_CROP_WIDTH, crop->width);
  533. height = clamp_val(ALIGN(format->format.height, 2),
  534. OV2680_MIN_CROP_HEIGHT, crop->height);
  535. ov2680_fill_format(sensor, &format->format, width, height);
  536. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  537. try_fmt = v4l2_subdev_state_get_format(sd_state, 0);
  538. *try_fmt = format->format;
  539. return 0;
  540. }
  541. mutex_lock(&sensor->lock);
  542. if (sensor->is_streaming) {
  543. ret = -EBUSY;
  544. goto unlock;
  545. }
  546. sensor->mode.fmt = format->format;
  547. ov2680_calc_mode(sensor);
  548. /* vblank range is height dependent adjust and reset to default */
  549. max = OV2680_MAX_VBLANK - height;
  550. def = OV2680_LINES_PER_FRAME_30FPS - height;
  551. ret = __v4l2_ctrl_modify_range(sensor->ctrls.vblank, OV2680_MIN_VBLANK,
  552. max, 1, def);
  553. if (ret)
  554. goto unlock;
  555. ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.vblank, def);
  556. if (ret)
  557. goto unlock;
  558. /* exposure range depends on vts which may have changed */
  559. ret = ov2680_exposure_update_range(sensor);
  560. if (ret)
  561. goto unlock;
  562. /* adjust hblank value for new width */
  563. def = OV2680_PIXELS_PER_LINE - width;
  564. ret = __v4l2_ctrl_modify_range(sensor->ctrls.hblank, def, def, 1, def);
  565. unlock:
  566. mutex_unlock(&sensor->lock);
  567. return ret;
  568. }
  569. static int ov2680_get_selection(struct v4l2_subdev *sd,
  570. struct v4l2_subdev_state *state,
  571. struct v4l2_subdev_selection *sel)
  572. {
  573. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  574. switch (sel->target) {
  575. case V4L2_SEL_TGT_CROP:
  576. mutex_lock(&sensor->lock);
  577. sel->r = *__ov2680_get_pad_crop(sensor, state, sel->pad,
  578. sel->which);
  579. mutex_unlock(&sensor->lock);
  580. break;
  581. case V4L2_SEL_TGT_NATIVE_SIZE:
  582. case V4L2_SEL_TGT_CROP_BOUNDS:
  583. sel->r.top = 0;
  584. sel->r.left = 0;
  585. sel->r.width = OV2680_NATIVE_WIDTH;
  586. sel->r.height = OV2680_NATIVE_HEIGHT;
  587. break;
  588. case V4L2_SEL_TGT_CROP_DEFAULT:
  589. sel->r = ov2680_default_crop;
  590. break;
  591. default:
  592. return -EINVAL;
  593. }
  594. return 0;
  595. }
  596. static int ov2680_set_selection(struct v4l2_subdev *sd,
  597. struct v4l2_subdev_state *state,
  598. struct v4l2_subdev_selection *sel)
  599. {
  600. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  601. struct v4l2_mbus_framefmt *format;
  602. struct v4l2_rect *crop;
  603. struct v4l2_rect rect;
  604. if (sel->target != V4L2_SEL_TGT_CROP)
  605. return -EINVAL;
  606. /*
  607. * Clamp the boundaries of the crop rectangle to the size of the sensor
  608. * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't
  609. * disrupted.
  610. */
  611. rect.left = clamp_val(ALIGN(sel->r.left, 2),
  612. OV2680_NATIVE_START_LEFT, OV2680_NATIVE_WIDTH);
  613. rect.top = clamp_val(ALIGN(sel->r.top, 2),
  614. OV2680_NATIVE_START_TOP, OV2680_NATIVE_HEIGHT);
  615. rect.width = clamp_val(ALIGN(sel->r.width, 2),
  616. OV2680_MIN_CROP_WIDTH, OV2680_NATIVE_WIDTH);
  617. rect.height = clamp_val(ALIGN(sel->r.height, 2),
  618. OV2680_MIN_CROP_HEIGHT, OV2680_NATIVE_HEIGHT);
  619. /* Make sure the crop rectangle isn't outside the bounds of the array */
  620. rect.width = min_t(unsigned int, rect.width,
  621. OV2680_NATIVE_WIDTH - rect.left);
  622. rect.height = min_t(unsigned int, rect.height,
  623. OV2680_NATIVE_HEIGHT - rect.top);
  624. crop = __ov2680_get_pad_crop(sensor, state, sel->pad, sel->which);
  625. mutex_lock(&sensor->lock);
  626. if (rect.width != crop->width || rect.height != crop->height) {
  627. /*
  628. * Reset the output image size if the crop rectangle size has
  629. * been modified.
  630. */
  631. format = __ov2680_get_pad_format(sensor, state, sel->pad,
  632. sel->which);
  633. format->width = rect.width;
  634. format->height = rect.height;
  635. }
  636. *crop = rect;
  637. mutex_unlock(&sensor->lock);
  638. sel->r = rect;
  639. return 0;
  640. }
  641. static int ov2680_init_state(struct v4l2_subdev *sd,
  642. struct v4l2_subdev_state *sd_state)
  643. {
  644. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  645. *v4l2_subdev_state_get_crop(sd_state, 0) = ov2680_default_crop;
  646. ov2680_fill_format(sensor, v4l2_subdev_state_get_format(sd_state, 0),
  647. OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
  648. return 0;
  649. }
  650. static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
  651. struct v4l2_subdev_state *sd_state,
  652. struct v4l2_subdev_frame_size_enum *fse)
  653. {
  654. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  655. struct v4l2_rect *crop;
  656. if (fse->index >= OV2680_FRAME_SIZES)
  657. return -EINVAL;
  658. crop = __ov2680_get_pad_crop(sensor, sd_state, fse->pad, fse->which);
  659. if (!crop)
  660. return -EINVAL;
  661. fse->min_width = crop->width / (fse->index + 1);
  662. fse->min_height = crop->height / (fse->index + 1);
  663. fse->max_width = fse->min_width;
  664. fse->max_height = fse->min_height;
  665. return 0;
  666. }
  667. static bool ov2680_valid_frame_size(struct v4l2_subdev *sd,
  668. struct v4l2_subdev_state *sd_state,
  669. struct v4l2_subdev_frame_interval_enum *fie)
  670. {
  671. struct v4l2_subdev_frame_size_enum fse = {
  672. .pad = fie->pad,
  673. .which = fie->which,
  674. };
  675. int i;
  676. for (i = 0; i < OV2680_FRAME_SIZES; i++) {
  677. fse.index = i;
  678. if (ov2680_enum_frame_size(sd, sd_state, &fse))
  679. return false;
  680. if (fie->width == fse.min_width &&
  681. fie->height == fse.min_height)
  682. return true;
  683. }
  684. return false;
  685. }
  686. static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
  687. struct v4l2_subdev_state *sd_state,
  688. struct v4l2_subdev_frame_interval_enum *fie)
  689. {
  690. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  691. /* Only 1 framerate */
  692. if (fie->index || !ov2680_valid_frame_size(sd, sd_state, fie))
  693. return -EINVAL;
  694. fie->interval = sensor->mode.frame_interval;
  695. return 0;
  696. }
  697. static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
  698. {
  699. struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
  700. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  701. int ret;
  702. /* Update exposure range on vblank changes */
  703. if (ctrl->id == V4L2_CID_VBLANK) {
  704. ret = ov2680_exposure_update_range(sensor);
  705. if (ret)
  706. return ret;
  707. }
  708. /* Only apply changes to the controls if the device is powered up */
  709. if (!pm_runtime_get_if_in_use(sensor->sd.dev)) {
  710. ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
  711. return 0;
  712. }
  713. switch (ctrl->id) {
  714. case V4L2_CID_ANALOGUE_GAIN:
  715. ret = ov2680_gain_set(sensor, ctrl->val);
  716. break;
  717. case V4L2_CID_EXPOSURE:
  718. ret = ov2680_exposure_set(sensor, ctrl->val);
  719. break;
  720. case V4L2_CID_VFLIP:
  721. ret = ov2680_set_vflip(sensor, ctrl->val);
  722. break;
  723. case V4L2_CID_HFLIP:
  724. ret = ov2680_set_hflip(sensor, ctrl->val);
  725. break;
  726. case V4L2_CID_TEST_PATTERN:
  727. ret = ov2680_test_pattern_set(sensor, ctrl->val);
  728. break;
  729. case V4L2_CID_VBLANK:
  730. ret = cci_write(sensor->regmap, OV2680_REG_TIMING_VTS,
  731. sensor->mode.fmt.height + ctrl->val, NULL);
  732. break;
  733. default:
  734. ret = -EINVAL;
  735. break;
  736. }
  737. pm_runtime_put(sensor->sd.dev);
  738. return ret;
  739. }
  740. static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
  741. .s_ctrl = ov2680_s_ctrl,
  742. };
  743. static const struct v4l2_subdev_video_ops ov2680_video_ops = {
  744. .s_stream = ov2680_s_stream,
  745. };
  746. static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
  747. .enum_mbus_code = ov2680_enum_mbus_code,
  748. .enum_frame_size = ov2680_enum_frame_size,
  749. .enum_frame_interval = ov2680_enum_frame_interval,
  750. .get_fmt = ov2680_get_fmt,
  751. .set_fmt = ov2680_set_fmt,
  752. .get_selection = ov2680_get_selection,
  753. .set_selection = ov2680_set_selection,
  754. .get_frame_interval = ov2680_get_frame_interval,
  755. .set_frame_interval = ov2680_get_frame_interval,
  756. };
  757. static const struct v4l2_subdev_ops ov2680_subdev_ops = {
  758. .video = &ov2680_video_ops,
  759. .pad = &ov2680_pad_ops,
  760. };
  761. static const struct v4l2_subdev_internal_ops ov2680_internal_ops = {
  762. .init_state = ov2680_init_state,
  763. };
  764. static int ov2680_mode_init(struct ov2680_dev *sensor)
  765. {
  766. /* set initial mode */
  767. sensor->mode.crop = ov2680_default_crop;
  768. ov2680_fill_format(sensor, &sensor->mode.fmt,
  769. OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
  770. ov2680_calc_mode(sensor);
  771. sensor->mode.frame_interval.denominator = OV2680_FRAME_RATE;
  772. sensor->mode.frame_interval.numerator = 1;
  773. return 0;
  774. }
  775. static int ov2680_v4l2_register(struct ov2680_dev *sensor)
  776. {
  777. struct i2c_client *client = to_i2c_client(sensor->dev);
  778. const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
  779. struct ov2680_ctrls *ctrls = &sensor->ctrls;
  780. struct v4l2_ctrl_handler *hdl = &ctrls->handler;
  781. struct v4l2_fwnode_device_properties props;
  782. int def, max, ret = 0;
  783. v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_subdev_ops);
  784. sensor->sd.internal_ops = &ov2680_internal_ops;
  785. sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
  786. sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
  787. sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  788. ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
  789. if (ret < 0)
  790. return ret;
  791. v4l2_ctrl_handler_init(hdl, 11);
  792. hdl->lock = &sensor->lock;
  793. ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  794. ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  795. ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
  796. &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
  797. ARRAY_SIZE(test_pattern_menu) - 1,
  798. 0, 0, test_pattern_menu);
  799. max = OV2680_LINES_PER_FRAME_30FPS - OV2680_INTEGRATION_TIME_MARGIN;
  800. ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
  801. 0, max, 1, max);
  802. ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_ANALOGUE_GAIN,
  803. 0, 1023, 1, 250);
  804. ctrls->link_freq = v4l2_ctrl_new_int_menu(hdl, NULL, V4L2_CID_LINK_FREQ,
  805. 0, 0, sensor->link_freq);
  806. ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, NULL, V4L2_CID_PIXEL_RATE,
  807. 0, sensor->pixel_rate,
  808. 1, sensor->pixel_rate);
  809. max = OV2680_MAX_VBLANK - OV2680_DEFAULT_HEIGHT;
  810. def = OV2680_LINES_PER_FRAME_30FPS - OV2680_DEFAULT_HEIGHT;
  811. ctrls->vblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VBLANK,
  812. OV2680_MIN_VBLANK, max, 1, def);
  813. def = OV2680_PIXELS_PER_LINE - OV2680_DEFAULT_WIDTH;
  814. ctrls->hblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HBLANK,
  815. def, def, 1, def);
  816. ret = v4l2_fwnode_device_parse(sensor->dev, &props);
  817. if (ret)
  818. goto cleanup_entity;
  819. v4l2_ctrl_new_fwnode_properties(hdl, ops, &props);
  820. if (hdl->error) {
  821. ret = hdl->error;
  822. goto cleanup_entity;
  823. }
  824. ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
  825. ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
  826. ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  827. ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  828. sensor->sd.ctrl_handler = hdl;
  829. ret = v4l2_async_register_subdev(&sensor->sd);
  830. if (ret < 0)
  831. goto cleanup_entity;
  832. return 0;
  833. cleanup_entity:
  834. media_entity_cleanup(&sensor->sd.entity);
  835. v4l2_ctrl_handler_free(hdl);
  836. return ret;
  837. }
  838. static int ov2680_get_regulators(struct ov2680_dev *sensor)
  839. {
  840. int i;
  841. for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
  842. sensor->supplies[i].supply = ov2680_supply_name[i];
  843. return devm_regulator_bulk_get(sensor->dev,
  844. OV2680_NUM_SUPPLIES, sensor->supplies);
  845. }
  846. static int ov2680_check_id(struct ov2680_dev *sensor)
  847. {
  848. u64 chip_id, rev;
  849. int ret = 0;
  850. cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, &ret);
  851. cci_read(sensor->regmap, OV2680_REG_SC_CMMN_SUB_ID, &rev, &ret);
  852. if (ret < 0) {
  853. dev_err(sensor->dev, "failed to read chip id\n");
  854. return ret;
  855. }
  856. if (chip_id != OV2680_CHIP_ID) {
  857. dev_err(sensor->dev, "chip id: 0x%04llx does not match expected 0x%04x\n",
  858. chip_id, OV2680_CHIP_ID);
  859. return -ENODEV;
  860. }
  861. dev_info(sensor->dev, "sensor_revision id = 0x%llx, rev= %lld\n",
  862. chip_id, rev & 0x0f);
  863. return 0;
  864. }
  865. static int ov2680_parse_dt(struct ov2680_dev *sensor)
  866. {
  867. struct v4l2_fwnode_endpoint bus_cfg = {
  868. .bus_type = V4L2_MBUS_CSI2_DPHY,
  869. };
  870. struct device *dev = sensor->dev;
  871. struct fwnode_handle *ep_fwnode;
  872. struct gpio_desc *gpio;
  873. int i, ret;
  874. /*
  875. * Sometimes the fwnode graph is initialized by the bridge driver.
  876. * Bridge drivers doing this may also add GPIO mappings, wait for this.
  877. */
  878. ep_fwnode = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
  879. if (!ep_fwnode)
  880. return dev_err_probe(dev, -EPROBE_DEFER,
  881. "waiting for fwnode graph endpoint\n");
  882. ret = v4l2_fwnode_endpoint_alloc_parse(ep_fwnode, &bus_cfg);
  883. fwnode_handle_put(ep_fwnode);
  884. if (ret)
  885. return ret;
  886. /*
  887. * The pin we want is named XSHUTDN in the datasheet. Linux sensor
  888. * drivers have standardized on using "powerdown" as con-id name
  889. * for powerdown or shutdown pins. Older DTB files use "reset",
  890. * so fallback to that if there is no "powerdown" pin.
  891. */
  892. gpio = devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
  893. if (!gpio)
  894. gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  895. ret = PTR_ERR_OR_ZERO(gpio);
  896. if (ret < 0) {
  897. dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
  898. goto out_free_bus_cfg;
  899. }
  900. sensor->pwdn_gpio = gpio;
  901. sensor->xvclk = devm_v4l2_sensor_clk_get(dev, "xvclk");
  902. if (IS_ERR(sensor->xvclk)) {
  903. ret = dev_err_probe(dev, PTR_ERR(sensor->xvclk),
  904. "xvclk clock missing or invalid\n");
  905. goto out_free_bus_cfg;
  906. }
  907. sensor->xvclk_freq = clk_get_rate(sensor->xvclk);
  908. for (i = 0; i < ARRAY_SIZE(ov2680_xvclk_freqs); i++) {
  909. if (sensor->xvclk_freq == ov2680_xvclk_freqs[i])
  910. break;
  911. }
  912. if (i == ARRAY_SIZE(ov2680_xvclk_freqs)) {
  913. ret = dev_err_probe(dev, -EINVAL,
  914. "unsupported xvclk frequency %d Hz\n",
  915. sensor->xvclk_freq);
  916. goto out_free_bus_cfg;
  917. }
  918. sensor->pll_mult = ov2680_pll_multipliers[i];
  919. sensor->link_freq[0] = sensor->xvclk_freq / OV2680_PLL_PREDIV0 /
  920. OV2680_PLL_PREDIV * sensor->pll_mult;
  921. /* CSI-2 is double data rate, bus-format is 10 bpp */
  922. sensor->pixel_rate = sensor->link_freq[0] * 2;
  923. do_div(sensor->pixel_rate, 10);
  924. if (!bus_cfg.nr_of_link_frequencies) {
  925. dev_warn(dev, "Consider passing 'link-frequencies' in DT\n");
  926. goto skip_link_freq_validation;
  927. }
  928. for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
  929. if (bus_cfg.link_frequencies[i] == sensor->link_freq[0])
  930. break;
  931. if (bus_cfg.nr_of_link_frequencies == i) {
  932. ret = dev_err_probe(dev, -EINVAL,
  933. "supported link freq %lld not found\n",
  934. sensor->link_freq[0]);
  935. goto out_free_bus_cfg;
  936. }
  937. skip_link_freq_validation:
  938. ret = 0;
  939. out_free_bus_cfg:
  940. v4l2_fwnode_endpoint_free(&bus_cfg);
  941. return ret;
  942. }
  943. static int ov2680_probe(struct i2c_client *client)
  944. {
  945. struct device *dev = &client->dev;
  946. struct ov2680_dev *sensor;
  947. int ret;
  948. sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
  949. if (!sensor)
  950. return -ENOMEM;
  951. sensor->dev = &client->dev;
  952. sensor->regmap = devm_cci_regmap_init_i2c(client, 16);
  953. if (IS_ERR(sensor->regmap))
  954. return PTR_ERR(sensor->regmap);
  955. ret = ov2680_parse_dt(sensor);
  956. if (ret < 0)
  957. return ret;
  958. ret = ov2680_mode_init(sensor);
  959. if (ret < 0)
  960. return ret;
  961. ret = ov2680_get_regulators(sensor);
  962. if (ret < 0) {
  963. dev_err(dev, "failed to get regulators\n");
  964. return ret;
  965. }
  966. mutex_init(&sensor->lock);
  967. /*
  968. * Power up and verify the chip now, so that if runtime pm is
  969. * disabled the chip is left on and streaming will work.
  970. */
  971. ret = ov2680_power_on(sensor);
  972. if (ret < 0)
  973. goto lock_destroy;
  974. ret = ov2680_check_id(sensor);
  975. if (ret < 0)
  976. goto err_powerdown;
  977. pm_runtime_set_active(&client->dev);
  978. pm_runtime_get_noresume(&client->dev);
  979. pm_runtime_enable(&client->dev);
  980. ret = ov2680_v4l2_register(sensor);
  981. if (ret < 0)
  982. goto err_pm_runtime;
  983. pm_runtime_set_autosuspend_delay(&client->dev, 1000);
  984. pm_runtime_use_autosuspend(&client->dev);
  985. pm_runtime_put_autosuspend(&client->dev);
  986. return 0;
  987. err_pm_runtime:
  988. pm_runtime_disable(&client->dev);
  989. pm_runtime_put_noidle(&client->dev);
  990. err_powerdown:
  991. ov2680_power_off(sensor);
  992. lock_destroy:
  993. dev_err(dev, "ov2680 init fail: %d\n", ret);
  994. mutex_destroy(&sensor->lock);
  995. return ret;
  996. }
  997. static void ov2680_remove(struct i2c_client *client)
  998. {
  999. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1000. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  1001. v4l2_async_unregister_subdev(&sensor->sd);
  1002. mutex_destroy(&sensor->lock);
  1003. media_entity_cleanup(&sensor->sd.entity);
  1004. v4l2_ctrl_handler_free(&sensor->ctrls.handler);
  1005. /*
  1006. * Disable runtime PM. In case runtime PM is disabled in the kernel,
  1007. * make sure to turn power off manually.
  1008. */
  1009. pm_runtime_disable(&client->dev);
  1010. if (!pm_runtime_status_suspended(&client->dev))
  1011. ov2680_power_off(sensor);
  1012. pm_runtime_set_suspended(&client->dev);
  1013. }
  1014. static int ov2680_suspend(struct device *dev)
  1015. {
  1016. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  1017. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  1018. if (sensor->is_streaming)
  1019. ov2680_stream_disable(sensor);
  1020. return ov2680_power_off(sensor);
  1021. }
  1022. static int ov2680_resume(struct device *dev)
  1023. {
  1024. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  1025. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  1026. int ret;
  1027. ret = ov2680_power_on(sensor);
  1028. if (ret < 0)
  1029. goto stream_disable;
  1030. if (sensor->is_streaming) {
  1031. ret = ov2680_stream_enable(sensor);
  1032. if (ret < 0)
  1033. goto stream_disable;
  1034. }
  1035. return 0;
  1036. stream_disable:
  1037. ov2680_stream_disable(sensor);
  1038. sensor->is_streaming = false;
  1039. return ret;
  1040. }
  1041. static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops, ov2680_suspend, ov2680_resume,
  1042. NULL);
  1043. static const struct of_device_id ov2680_dt_ids[] = {
  1044. { .compatible = "ovti,ov2680" },
  1045. { /* sentinel */ },
  1046. };
  1047. MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
  1048. static const struct acpi_device_id ov2680_acpi_ids[] = {
  1049. { "OVTI2680" },
  1050. { /* sentinel */ }
  1051. };
  1052. MODULE_DEVICE_TABLE(acpi, ov2680_acpi_ids);
  1053. static struct i2c_driver ov2680_i2c_driver = {
  1054. .driver = {
  1055. .name = "ov2680",
  1056. .pm = pm_sleep_ptr(&ov2680_pm_ops),
  1057. .of_match_table = ov2680_dt_ids,
  1058. .acpi_match_table = ov2680_acpi_ids,
  1059. },
  1060. .probe = ov2680_probe,
  1061. .remove = ov2680_remove,
  1062. };
  1063. module_i2c_driver(ov2680_i2c_driver);
  1064. MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
  1065. MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
  1066. MODULE_LICENSE("GPL v2");