ov9282.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * OmniVision ov9282 Camera Sensor Driver
  4. *
  5. * Copyright (C) 2021 Intel Corporation
  6. */
  7. #include <linux/unaligned.h>
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/i2c.h>
  11. #include <linux/math.h>
  12. #include <linux/module.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <media/v4l2-ctrls.h>
  16. #include <media/v4l2-event.h>
  17. #include <media/v4l2-fwnode.h>
  18. #include <media/v4l2-subdev.h>
  19. /* Streaming Mode */
  20. #define OV9282_REG_MODE_SELECT 0x0100
  21. #define OV9282_MODE_STANDBY 0x00
  22. #define OV9282_MODE_STREAMING 0x01
  23. #define OV9282_REG_PLL_CTRL_0D 0x030d
  24. #define OV9282_PLL_CTRL_0D_RAW8 0x60
  25. #define OV9282_PLL_CTRL_0D_RAW10 0x50
  26. #define OV9282_REG_TIMING_HTS 0x380c
  27. #define OV9282_TIMING_HTS_MAX 0x7fff
  28. /* Lines per frame */
  29. #define OV9282_REG_LPFR 0x380e
  30. /* Chip ID */
  31. #define OV9282_REG_ID 0x300a
  32. #define OV9282_ID 0x9281
  33. /* Output enable registers */
  34. #define OV9282_REG_OUTPUT_ENABLE4 0x3004
  35. #define OV9282_OUTPUT_ENABLE4_GPIO2 BIT(1)
  36. #define OV9282_OUTPUT_ENABLE4_D9 BIT(0)
  37. #define OV9282_REG_OUTPUT_ENABLE5 0x3005
  38. #define OV9282_OUTPUT_ENABLE5_D8 BIT(7)
  39. #define OV9282_OUTPUT_ENABLE5_D7 BIT(6)
  40. #define OV9282_OUTPUT_ENABLE5_D6 BIT(5)
  41. #define OV9282_OUTPUT_ENABLE5_D5 BIT(4)
  42. #define OV9282_OUTPUT_ENABLE5_D4 BIT(3)
  43. #define OV9282_OUTPUT_ENABLE5_D3 BIT(2)
  44. #define OV9282_OUTPUT_ENABLE5_D2 BIT(1)
  45. #define OV9282_OUTPUT_ENABLE5_D1 BIT(0)
  46. #define OV9282_REG_OUTPUT_ENABLE6 0x3006
  47. #define OV9282_OUTPUT_ENABLE6_D0 BIT(7)
  48. #define OV9282_OUTPUT_ENABLE6_PCLK BIT(6)
  49. #define OV9282_OUTPUT_ENABLE6_HREF BIT(5)
  50. #define OV9282_OUTPUT_ENABLE6_STROBE BIT(3)
  51. #define OV9282_OUTPUT_ENABLE6_ILPWM BIT(2)
  52. #define OV9282_OUTPUT_ENABLE6_VSYNC BIT(1)
  53. /* Exposure control */
  54. #define OV9282_REG_EXPOSURE 0x3500
  55. #define OV9282_EXPOSURE_MIN 1
  56. #define OV9282_EXPOSURE_OFFSET 25
  57. #define OV9282_EXPOSURE_STEP 1
  58. #define OV9282_EXPOSURE_DEFAULT 0x0282
  59. /* AEC/AGC manual */
  60. #define OV9282_REG_AEC_MANUAL 0x3503
  61. #define OV9282_DIGFRAC_GAIN_DELAY BIT(6)
  62. #define OV9282_GAIN_CHANGE_DELAY BIT(5)
  63. #define OV9282_GAIN_DELAY BIT(4)
  64. #define OV9282_GAIN_PREC16_EN BIT(3)
  65. #define OV9282_GAIN_MANUAL_AS_SENSGAIN BIT(2)
  66. #define OV9282_AEC_MANUAL_DEFAULT 0x00
  67. /* Analog gain control */
  68. #define OV9282_REG_AGAIN 0x3509
  69. #define OV9282_AGAIN_MIN 0x10
  70. #define OV9282_AGAIN_MAX 0xff
  71. #define OV9282_AGAIN_STEP 1
  72. #define OV9282_AGAIN_DEFAULT 0x10
  73. /* Group hold register */
  74. #define OV9282_REG_HOLD 0x3308
  75. #define OV9282_REG_ANA_CORE_2 0x3662
  76. #define OV9282_ANA_CORE2_RAW8 0x07
  77. #define OV9282_ANA_CORE2_RAW10 0x05
  78. #define OV9282_REG_TIMING_FORMAT_1 0x3820
  79. #define OV9282_REG_TIMING_FORMAT_2 0x3821
  80. #define OV9282_FLIP_BIT BIT(2)
  81. #define OV9282_REG_MIPI_CTRL00 0x4800
  82. #define OV9282_GATED_CLOCK BIT(5)
  83. /* Flash/Strobe control registers */
  84. #define OV9282_REG_STROBE_FRAME_SPAN 0x3925
  85. #define OV9282_STROBE_FRAME_SPAN_DEFAULT 0x0000001a
  86. /* Input clock rate */
  87. #define OV9282_INCLK_RATE 24000000
  88. /* CSI2 HW configuration */
  89. #define OV9282_LINK_FREQ 400000000
  90. #define OV9282_NUM_DATA_LANES 2
  91. /* Pixel rate */
  92. #define OV9282_PIXEL_RATE_10BIT (OV9282_LINK_FREQ * 2 * \
  93. OV9282_NUM_DATA_LANES / 10)
  94. #define OV9282_PIXEL_RATE_8BIT (OV9282_LINK_FREQ * 2 * \
  95. OV9282_NUM_DATA_LANES / 8)
  96. /*
  97. * OV9282 native and active pixel array size.
  98. * 8 dummy rows/columns on each edge of a 1280x800 active array
  99. */
  100. #define OV9282_NATIVE_WIDTH 1296U
  101. #define OV9282_NATIVE_HEIGHT 816U
  102. #define OV9282_PIXEL_ARRAY_LEFT 8U
  103. #define OV9282_PIXEL_ARRAY_TOP 8U
  104. #define OV9282_PIXEL_ARRAY_WIDTH 1280U
  105. #define OV9282_PIXEL_ARRAY_HEIGHT 800U
  106. #define OV9282_REG_MIN 0x00
  107. #define OV9282_REG_MAX 0xfffff
  108. #define OV9282_STROBE_SPAN_FACTOR 192
  109. static const char * const ov9282_supply_names[] = {
  110. "avdd", /* Analog power */
  111. "dovdd", /* Digital I/O power */
  112. "dvdd", /* Digital core power */
  113. };
  114. #define OV9282_NUM_SUPPLIES ARRAY_SIZE(ov9282_supply_names)
  115. /**
  116. * struct ov9282_reg - ov9282 sensor register
  117. * @address: Register address
  118. * @val: Register value
  119. */
  120. struct ov9282_reg {
  121. u16 address;
  122. u8 val;
  123. };
  124. /**
  125. * struct ov9282_reg_list - ov9282 sensor register list
  126. * @num_of_regs: Number of registers in the list
  127. * @regs: Pointer to register list
  128. */
  129. struct ov9282_reg_list {
  130. u32 num_of_regs;
  131. const struct ov9282_reg *regs;
  132. };
  133. /**
  134. * struct ov9282_mode - ov9282 sensor mode structure
  135. * @width: Frame width
  136. * @height: Frame height
  137. * @hblank_min: Minimum horizontal blanking in lines for non-continuous[0] and
  138. * continuous[1] clock modes
  139. * @vblank: Vertical blanking in lines
  140. * @vblank_min: Minimum vertical blanking in lines
  141. * @vblank_max: Maximum vertical blanking in lines
  142. * @link_freq_idx: Link frequency index
  143. * @crop: on-sensor cropping for this mode
  144. * @reg_list: Register list for sensor mode
  145. */
  146. struct ov9282_mode {
  147. u32 width;
  148. u32 height;
  149. u32 hblank_min[2];
  150. u32 vblank;
  151. u32 vblank_min;
  152. u32 vblank_max;
  153. u32 link_freq_idx;
  154. struct v4l2_rect crop;
  155. struct ov9282_reg_list reg_list;
  156. };
  157. /**
  158. * struct ov9282 - ov9282 sensor device structure
  159. * @dev: Pointer to generic device
  160. * @sd: V4L2 sub-device
  161. * @pad: Media pad. Only one pad supported
  162. * @reset_gpio: Sensor reset gpio
  163. * @inclk: Sensor input clock
  164. * @supplies: Regulator supplies for the sensor
  165. * @ctrl_handler: V4L2 control handler
  166. * @link_freq_ctrl: Pointer to link frequency control
  167. * @hblank_ctrl: Pointer to horizontal blanking control
  168. * @vblank_ctrl: Pointer to vertical blanking control
  169. * @exp_ctrl: Pointer to exposure control
  170. * @again_ctrl: Pointer to analog gain control
  171. * @pixel_rate: Pointer to pixel rate control
  172. * @flash_duration: Pointer to flash duration control
  173. * @vblank: Vertical blanking in lines
  174. * @noncontinuous_clock: Selection of CSI2 noncontinuous clock mode
  175. * @cur_mode: Pointer to current selected sensor mode
  176. * @code: Mbus code currently selected
  177. * @mutex: Mutex for serializing sensor controls
  178. */
  179. struct ov9282 {
  180. struct device *dev;
  181. struct v4l2_subdev sd;
  182. struct media_pad pad;
  183. struct gpio_desc *reset_gpio;
  184. struct clk *inclk;
  185. struct regulator_bulk_data supplies[OV9282_NUM_SUPPLIES];
  186. struct v4l2_ctrl_handler ctrl_handler;
  187. struct v4l2_ctrl *link_freq_ctrl;
  188. struct v4l2_ctrl *hblank_ctrl;
  189. struct v4l2_ctrl *vblank_ctrl;
  190. struct {
  191. struct v4l2_ctrl *exp_ctrl;
  192. struct v4l2_ctrl *again_ctrl;
  193. };
  194. struct v4l2_ctrl *pixel_rate;
  195. struct v4l2_ctrl *flash_duration;
  196. u32 vblank;
  197. bool noncontinuous_clock;
  198. const struct ov9282_mode *cur_mode;
  199. u32 code;
  200. struct mutex mutex;
  201. };
  202. static const s64 link_freq[] = {
  203. OV9282_LINK_FREQ,
  204. };
  205. /*
  206. * Common registers
  207. *
  208. * Note: Do NOT include a software reset (0x0103, 0x01) in any of these
  209. * register arrays as some settings are written as part of ov9282_power_on,
  210. * and the reset will clear them.
  211. */
  212. static const struct ov9282_reg common_regs[] = {
  213. {0x0302, 0x32},
  214. {0x030e, 0x02},
  215. {0x3001, 0x00},
  216. {OV9282_REG_OUTPUT_ENABLE4, 0x00},
  217. {OV9282_REG_OUTPUT_ENABLE5, 0x00},
  218. {OV9282_REG_OUTPUT_ENABLE6, OV9282_OUTPUT_ENABLE6_ILPWM},
  219. {0x3011, 0x0a},
  220. {0x3013, 0x18},
  221. {0x301c, 0xf0},
  222. {0x3022, 0x01},
  223. {0x3030, 0x10},
  224. {0x3039, 0x32},
  225. {0x303a, 0x00},
  226. {OV9282_REG_AEC_MANUAL, OV9282_GAIN_PREC16_EN},
  227. {0x3505, 0x8c},
  228. {0x3507, 0x03},
  229. {0x3508, 0x00},
  230. {0x3610, 0x80},
  231. {0x3611, 0xa0},
  232. {0x3620, 0x6e},
  233. {0x3632, 0x56},
  234. {0x3633, 0x78},
  235. {0x3666, 0x00},
  236. {0x366f, 0x5a},
  237. {0x3680, 0x84},
  238. {0x3712, 0x80},
  239. {0x372d, 0x22},
  240. {0x3731, 0x80},
  241. {0x3732, 0x30},
  242. {0x377d, 0x22},
  243. {0x3788, 0x02},
  244. {0x3789, 0xa4},
  245. {0x378a, 0x00},
  246. {0x378b, 0x4a},
  247. {0x3799, 0x20},
  248. {0x3881, 0x42},
  249. {0x38a8, 0x02},
  250. {0x38a9, 0x80},
  251. {0x38b1, 0x00},
  252. {0x38c4, 0x00},
  253. {0x38c5, 0xc0},
  254. {0x38c6, 0x04},
  255. {0x38c7, 0x80},
  256. {0x3920, 0xff},
  257. {0x4010, 0x40},
  258. {0x4043, 0x40},
  259. {0x4307, 0x30},
  260. {0x4317, 0x00},
  261. {0x4501, 0x00},
  262. {0x450a, 0x08},
  263. {0x4601, 0x04},
  264. {0x470f, 0x00},
  265. {0x4f07, 0x00},
  266. {0x5000, 0x9f},
  267. {0x5001, 0x00},
  268. {0x5e00, 0x00},
  269. {0x5d00, 0x07},
  270. {0x5d01, 0x00},
  271. {0x0101, 0x01},
  272. {0x1000, 0x03},
  273. {0x5a08, 0x84},
  274. };
  275. static struct ov9282_reg_list common_regs_list = {
  276. .num_of_regs = ARRAY_SIZE(common_regs),
  277. .regs = common_regs,
  278. };
  279. #define MODE_1280_800 0
  280. #define MODE_1280_720 1
  281. #define MODE_640_400 2
  282. #define DEFAULT_MODE MODE_1280_720
  283. /* Sensor mode registers */
  284. static const struct ov9282_reg mode_1280x800_regs[] = {
  285. {0x3778, 0x00},
  286. {0x3800, 0x00},
  287. {0x3801, 0x00},
  288. {0x3802, 0x00},
  289. {0x3803, 0x00},
  290. {0x3804, 0x05},
  291. {0x3805, 0x0f},
  292. {0x3806, 0x03},
  293. {0x3807, 0x2f},
  294. {0x3808, 0x05},
  295. {0x3809, 0x00},
  296. {0x380a, 0x03},
  297. {0x380b, 0x20},
  298. {0x3810, 0x00},
  299. {0x3811, 0x08},
  300. {0x3812, 0x00},
  301. {0x3813, 0x08},
  302. {0x3814, 0x11},
  303. {0x3815, 0x11},
  304. {OV9282_REG_TIMING_FORMAT_1, 0x40},
  305. {OV9282_REG_TIMING_FORMAT_2, 0x00},
  306. {0x4003, 0x40},
  307. {0x4008, 0x04},
  308. {0x4009, 0x0b},
  309. {0x400c, 0x00},
  310. {0x400d, 0x07},
  311. {0x4507, 0x00},
  312. {0x4509, 0x00},
  313. };
  314. static const struct ov9282_reg mode_1280x720_regs[] = {
  315. {0x3778, 0x00},
  316. {0x3800, 0x00},
  317. {0x3801, 0x00},
  318. {0x3802, 0x00},
  319. {0x3803, 0x00},
  320. {0x3804, 0x05},
  321. {0x3805, 0x0f},
  322. {0x3806, 0x02},
  323. {0x3807, 0xdf},
  324. {0x3808, 0x05},
  325. {0x3809, 0x00},
  326. {0x380a, 0x02},
  327. {0x380b, 0xd0},
  328. {0x3810, 0x00},
  329. {0x3811, 0x08},
  330. {0x3812, 0x00},
  331. {0x3813, 0x08},
  332. {0x3814, 0x11},
  333. {0x3815, 0x11},
  334. {OV9282_REG_TIMING_FORMAT_1, 0x3c},
  335. {OV9282_REG_TIMING_FORMAT_2, 0x84},
  336. {0x4003, 0x40},
  337. {0x4008, 0x02},
  338. {0x4009, 0x05},
  339. {0x400c, 0x00},
  340. {0x400d, 0x03},
  341. {0x4507, 0x00},
  342. {0x4509, 0x80},
  343. };
  344. static const struct ov9282_reg mode_640x400_regs[] = {
  345. {0x3778, 0x10},
  346. {0x3800, 0x00},
  347. {0x3801, 0x00},
  348. {0x3802, 0x00},
  349. {0x3803, 0x00},
  350. {0x3804, 0x05},
  351. {0x3805, 0x0f},
  352. {0x3806, 0x03},
  353. {0x3807, 0x2f},
  354. {0x3808, 0x02},
  355. {0x3809, 0x80},
  356. {0x380a, 0x01},
  357. {0x380b, 0x90},
  358. {0x3810, 0x00},
  359. {0x3811, 0x04},
  360. {0x3812, 0x00},
  361. {0x3813, 0x04},
  362. {0x3814, 0x31},
  363. {0x3815, 0x22},
  364. {OV9282_REG_TIMING_FORMAT_1, 0x60},
  365. {OV9282_REG_TIMING_FORMAT_2, 0x01},
  366. {0x4008, 0x02},
  367. {0x4009, 0x05},
  368. {0x400c, 0x00},
  369. {0x400d, 0x03},
  370. {0x4507, 0x03},
  371. {0x4509, 0x80},
  372. };
  373. /* Supported sensor mode configurations */
  374. static const struct ov9282_mode supported_modes[] = {
  375. [MODE_1280_800] = {
  376. .width = 1280,
  377. .height = 800,
  378. .hblank_min = { 250, 176 },
  379. .vblank = 1022,
  380. .vblank_min = 110,
  381. .vblank_max = 51540,
  382. .link_freq_idx = 0,
  383. .crop = {
  384. .left = OV9282_PIXEL_ARRAY_LEFT,
  385. .top = OV9282_PIXEL_ARRAY_TOP,
  386. .width = 1280,
  387. .height = 800
  388. },
  389. .reg_list = {
  390. .num_of_regs = ARRAY_SIZE(mode_1280x800_regs),
  391. .regs = mode_1280x800_regs,
  392. },
  393. },
  394. [MODE_1280_720] = {
  395. .width = 1280,
  396. .height = 720,
  397. .hblank_min = { 250, 176 },
  398. .vblank = 1022,
  399. .vblank_min = 41,
  400. .vblank_max = 51540,
  401. .link_freq_idx = 0,
  402. .crop = {
  403. /*
  404. * Note that this mode takes the top 720 lines from the
  405. * 800 of the sensor. It does not take a middle crop.
  406. */
  407. .left = OV9282_PIXEL_ARRAY_LEFT,
  408. .top = OV9282_PIXEL_ARRAY_TOP,
  409. .width = 1280,
  410. .height = 720
  411. },
  412. .reg_list = {
  413. .num_of_regs = ARRAY_SIZE(mode_1280x720_regs),
  414. .regs = mode_1280x720_regs,
  415. },
  416. },
  417. [MODE_640_400] = {
  418. .width = 640,
  419. .height = 400,
  420. .hblank_min = { 890, 816 },
  421. .vblank = 1022,
  422. .vblank_min = 22,
  423. .vblank_max = 51540,
  424. .link_freq_idx = 0,
  425. .crop = {
  426. .left = OV9282_PIXEL_ARRAY_LEFT,
  427. .top = OV9282_PIXEL_ARRAY_TOP,
  428. .width = 1280,
  429. .height = 800
  430. },
  431. .reg_list = {
  432. .num_of_regs = ARRAY_SIZE(mode_640x400_regs),
  433. .regs = mode_640x400_regs,
  434. },
  435. },
  436. };
  437. /**
  438. * to_ov9282() - ov9282 V4L2 sub-device to ov9282 device.
  439. * @subdev: pointer to ov9282 V4L2 sub-device
  440. *
  441. * Return: pointer to ov9282 device
  442. */
  443. static inline struct ov9282 *to_ov9282(struct v4l2_subdev *subdev)
  444. {
  445. return container_of(subdev, struct ov9282, sd);
  446. }
  447. /**
  448. * ov9282_read_reg() - Read registers.
  449. * @ov9282: pointer to ov9282 device
  450. * @reg: register address
  451. * @len: length of bytes to read. Max supported bytes is 4
  452. * @val: pointer to register value to be filled.
  453. *
  454. * Return: 0 if successful, error code otherwise.
  455. */
  456. static int ov9282_read_reg(struct ov9282 *ov9282, u16 reg, u32 len, u32 *val)
  457. {
  458. struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd);
  459. struct i2c_msg msgs[2] = {0};
  460. u8 addr_buf[2] = {0};
  461. u8 data_buf[4] = {0};
  462. int ret;
  463. if (WARN_ON(len > 4))
  464. return -EINVAL;
  465. put_unaligned_be16(reg, addr_buf);
  466. /* Write register address */
  467. msgs[0].addr = client->addr;
  468. msgs[0].flags = 0;
  469. msgs[0].len = ARRAY_SIZE(addr_buf);
  470. msgs[0].buf = addr_buf;
  471. /* Read data from register */
  472. msgs[1].addr = client->addr;
  473. msgs[1].flags = I2C_M_RD;
  474. msgs[1].len = len;
  475. msgs[1].buf = &data_buf[4 - len];
  476. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  477. if (ret != ARRAY_SIZE(msgs))
  478. return -EIO;
  479. *val = get_unaligned_be32(data_buf);
  480. return 0;
  481. }
  482. /**
  483. * ov9282_write_reg() - Write register
  484. * @ov9282: pointer to ov9282 device
  485. * @reg: register address
  486. * @len: length of bytes. Max supported bytes is 4
  487. * @val: register value
  488. *
  489. * Return: 0 if successful, error code otherwise.
  490. */
  491. static int ov9282_write_reg(struct ov9282 *ov9282, u16 reg, u32 len, u32 val)
  492. {
  493. struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd);
  494. u8 buf[6] = {0};
  495. if (WARN_ON(len > 4))
  496. return -EINVAL;
  497. put_unaligned_be16(reg, buf);
  498. put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
  499. if (i2c_master_send(client, buf, len + 2) != len + 2)
  500. return -EIO;
  501. return 0;
  502. }
  503. /**
  504. * ov9282_write_regs() - Write a list of registers
  505. * @ov9282: pointer to ov9282 device
  506. * @regs: list of registers to be written
  507. * @len: length of registers array
  508. *
  509. * Return: 0 if successful, error code otherwise.
  510. */
  511. static int ov9282_write_regs(struct ov9282 *ov9282,
  512. const struct ov9282_reg *regs, u32 len)
  513. {
  514. unsigned int i;
  515. int ret;
  516. for (i = 0; i < len; i++) {
  517. ret = ov9282_write_reg(ov9282, regs[i].address, 1, regs[i].val);
  518. if (ret)
  519. return ret;
  520. }
  521. return 0;
  522. }
  523. /**
  524. * ov9282_update_controls() - Update control ranges based on streaming mode
  525. * @ov9282: pointer to ov9282 device
  526. * @mode: pointer to ov9282_mode sensor mode
  527. * @fmt: pointer to the requested mode
  528. *
  529. * Return: 0 if successful, error code otherwise.
  530. */
  531. static int ov9282_update_controls(struct ov9282 *ov9282,
  532. const struct ov9282_mode *mode,
  533. const struct v4l2_subdev_format *fmt)
  534. {
  535. u32 hblank_min;
  536. s64 pixel_rate;
  537. int ret;
  538. ret = __v4l2_ctrl_s_ctrl(ov9282->link_freq_ctrl, mode->link_freq_idx);
  539. if (ret)
  540. return ret;
  541. pixel_rate = (fmt->format.code == MEDIA_BUS_FMT_Y10_1X10) ?
  542. OV9282_PIXEL_RATE_10BIT : OV9282_PIXEL_RATE_8BIT;
  543. ret = __v4l2_ctrl_modify_range(ov9282->pixel_rate, pixel_rate,
  544. pixel_rate, 1, pixel_rate);
  545. if (ret)
  546. return ret;
  547. hblank_min = mode->hblank_min[ov9282->noncontinuous_clock ? 0 : 1];
  548. ret = __v4l2_ctrl_modify_range(ov9282->hblank_ctrl, hblank_min,
  549. OV9282_TIMING_HTS_MAX - mode->width, 1,
  550. hblank_min);
  551. if (ret)
  552. return ret;
  553. return __v4l2_ctrl_modify_range(ov9282->vblank_ctrl, mode->vblank_min,
  554. mode->vblank_max, 1, mode->vblank);
  555. }
  556. static u32 ov9282_exposure_to_us(struct ov9282 *ov9282, u32 exposure)
  557. {
  558. /* calculate exposure time in µs */
  559. u32 frame_width = ov9282->cur_mode->width + ov9282->hblank_ctrl->val;
  560. u32 trow_us = frame_width / (ov9282->pixel_rate->val / 1000000UL);
  561. return exposure * trow_us;
  562. }
  563. /**
  564. * ov9282_update_exp_gain() - Set updated exposure and gain
  565. * @ov9282: pointer to ov9282 device
  566. * @exposure: updated exposure value
  567. * @gain: updated analog gain value
  568. *
  569. * Return: 0 if successful, error code otherwise.
  570. */
  571. static int ov9282_update_exp_gain(struct ov9282 *ov9282, u32 exposure, u32 gain)
  572. {
  573. int ret;
  574. u32 exposure_us = ov9282_exposure_to_us(ov9282, exposure);
  575. dev_dbg(ov9282->dev, "Set exp %u (~%u us), analog gain %u",
  576. exposure, exposure_us, gain);
  577. ret = ov9282_write_reg(ov9282, OV9282_REG_HOLD, 1, 1);
  578. if (ret)
  579. return ret;
  580. ret = ov9282_write_reg(ov9282, OV9282_REG_EXPOSURE, 3, exposure << 4);
  581. if (ret)
  582. goto error_release_group_hold;
  583. ret = ov9282_write_reg(ov9282, OV9282_REG_AGAIN, 1, gain);
  584. if (ret)
  585. goto error_release_group_hold;
  586. ret = __v4l2_ctrl_modify_range(ov9282->flash_duration,
  587. 0, exposure_us, 1,
  588. OV9282_STROBE_FRAME_SPAN_DEFAULT);
  589. error_release_group_hold:
  590. ov9282_write_reg(ov9282, OV9282_REG_HOLD, 1, 0);
  591. return ret;
  592. }
  593. static int ov9282_set_ctrl_hflip(struct ov9282 *ov9282, int value)
  594. {
  595. u32 current_val;
  596. int ret = ov9282_read_reg(ov9282, OV9282_REG_TIMING_FORMAT_2, 1,
  597. &current_val);
  598. if (ret)
  599. return ret;
  600. if (value)
  601. current_val |= OV9282_FLIP_BIT;
  602. else
  603. current_val &= ~OV9282_FLIP_BIT;
  604. return ov9282_write_reg(ov9282, OV9282_REG_TIMING_FORMAT_2, 1,
  605. current_val);
  606. }
  607. static int ov9282_set_ctrl_vflip(struct ov9282 *ov9282, int value)
  608. {
  609. u32 current_val;
  610. int ret = ov9282_read_reg(ov9282, OV9282_REG_TIMING_FORMAT_1, 1,
  611. &current_val);
  612. if (ret)
  613. return ret;
  614. if (value)
  615. current_val |= OV9282_FLIP_BIT;
  616. else
  617. current_val &= ~OV9282_FLIP_BIT;
  618. return ov9282_write_reg(ov9282, OV9282_REG_TIMING_FORMAT_1, 1,
  619. current_val);
  620. }
  621. static int ov9282_set_ctrl_flash_strobe_oe(struct ov9282 *ov9282, bool enable)
  622. {
  623. u32 current_val;
  624. int ret;
  625. ret = ov9282_read_reg(ov9282, OV9282_REG_OUTPUT_ENABLE6, 1, &current_val);
  626. if (ret)
  627. return ret;
  628. if (enable)
  629. current_val |= OV9282_OUTPUT_ENABLE6_STROBE;
  630. else
  631. current_val &= ~OV9282_OUTPUT_ENABLE6_STROBE;
  632. return ov9282_write_reg(ov9282, OV9282_REG_OUTPUT_ENABLE6, 1, current_val);
  633. }
  634. static u32 ov9282_us_to_flash_duration(struct ov9282 *ov9282, u32 value)
  635. {
  636. /*
  637. * Calculate "strobe_frame_span" increments from a given value (µs).
  638. * This is quite tricky as "The step width of shift and span is
  639. * programmable under system clock domain.", but it's not documented
  640. * how to program this step width (at least in the datasheet available
  641. * to the author at time of writing).
  642. * The formula below is interpolated from different modes/framerates
  643. * and should work quite well for most settings.
  644. */
  645. u32 frame_width = ov9282->cur_mode->width + ov9282->hblank_ctrl->val;
  646. return value * OV9282_STROBE_SPAN_FACTOR / frame_width;
  647. }
  648. static u32 ov9282_flash_duration_to_us(struct ov9282 *ov9282, u32 value)
  649. {
  650. /*
  651. * Calculate back to microseconds from "strobe_frame_span" increments.
  652. * As the calculation in ov9282_us_to_flash_duration uses an integer
  653. * divison round up here.
  654. */
  655. u32 frame_width = ov9282->cur_mode->width + ov9282->hblank_ctrl->val;
  656. return DIV_ROUND_UP(value * frame_width, OV9282_STROBE_SPAN_FACTOR);
  657. }
  658. static int ov9282_set_ctrl_flash_duration(struct ov9282 *ov9282, u32 value)
  659. {
  660. u32 val = ov9282_us_to_flash_duration(ov9282, value);
  661. int ret;
  662. ret = ov9282_write_reg(ov9282, OV9282_REG_STROBE_FRAME_SPAN, 1,
  663. (val >> 24) & 0xff);
  664. if (ret)
  665. return ret;
  666. ret = ov9282_write_reg(ov9282, OV9282_REG_STROBE_FRAME_SPAN + 1, 1,
  667. (val >> 16) & 0xff);
  668. if (ret)
  669. return ret;
  670. ret = ov9282_write_reg(ov9282, OV9282_REG_STROBE_FRAME_SPAN + 2, 1,
  671. (val >> 8) & 0xff);
  672. if (ret)
  673. return ret;
  674. return ov9282_write_reg(ov9282, OV9282_REG_STROBE_FRAME_SPAN + 3, 1,
  675. val & 0xff);
  676. }
  677. /**
  678. * ov9282_set_ctrl() - Set subdevice control
  679. * @ctrl: pointer to v4l2_ctrl structure
  680. *
  681. * Supported controls:
  682. * - V4L2_CID_VBLANK
  683. * - cluster controls:
  684. * - V4L2_CID_ANALOGUE_GAIN
  685. * - V4L2_CID_EXPOSURE
  686. *
  687. * Return: 0 if successful, error code otherwise.
  688. */
  689. static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
  690. {
  691. struct ov9282 *ov9282 =
  692. container_of(ctrl->handler, struct ov9282, ctrl_handler);
  693. u32 analog_gain;
  694. u32 exposure;
  695. u32 lpfr;
  696. int ret;
  697. switch (ctrl->id) {
  698. case V4L2_CID_VBLANK:
  699. ov9282->vblank = ov9282->vblank_ctrl->val;
  700. dev_dbg(ov9282->dev, "Received vblank %u, new lpfr %u",
  701. ov9282->vblank,
  702. ov9282->vblank + ov9282->cur_mode->height);
  703. ret = __v4l2_ctrl_modify_range(ov9282->exp_ctrl,
  704. OV9282_EXPOSURE_MIN,
  705. ov9282->vblank +
  706. ov9282->cur_mode->height -
  707. OV9282_EXPOSURE_OFFSET,
  708. 1, OV9282_EXPOSURE_DEFAULT);
  709. break;
  710. }
  711. /* Set controls only if sensor is in power on state */
  712. if (!pm_runtime_get_if_in_use(ov9282->dev))
  713. return 0;
  714. switch (ctrl->id) {
  715. case V4L2_CID_EXPOSURE:
  716. exposure = ctrl->val;
  717. analog_gain = ov9282->again_ctrl->val;
  718. dev_dbg(ov9282->dev, "Received exp %u, analog gain %u",
  719. exposure, analog_gain);
  720. ret = ov9282_update_exp_gain(ov9282, exposure, analog_gain);
  721. break;
  722. case V4L2_CID_VBLANK:
  723. lpfr = ov9282->vblank + ov9282->cur_mode->height;
  724. ret = ov9282_write_reg(ov9282, OV9282_REG_LPFR, 2, lpfr);
  725. break;
  726. case V4L2_CID_HFLIP:
  727. ret = ov9282_set_ctrl_hflip(ov9282, ctrl->val);
  728. break;
  729. case V4L2_CID_VFLIP:
  730. ret = ov9282_set_ctrl_vflip(ov9282, ctrl->val);
  731. break;
  732. case V4L2_CID_HBLANK:
  733. ret = ov9282_write_reg(ov9282, OV9282_REG_TIMING_HTS, 2,
  734. (ctrl->val + ov9282->cur_mode->width) >> 1);
  735. break;
  736. case V4L2_CID_FLASH_STROBE_OE:
  737. ret = ov9282_set_ctrl_flash_strobe_oe(ov9282, ctrl->val);
  738. break;
  739. case V4L2_CID_FLASH_DURATION:
  740. ret = ov9282_set_ctrl_flash_duration(ov9282, ctrl->val);
  741. break;
  742. default:
  743. dev_err(ov9282->dev, "Invalid control %d", ctrl->id);
  744. ret = -EINVAL;
  745. }
  746. pm_runtime_put(ov9282->dev);
  747. return ret;
  748. }
  749. static int ov9282_try_ctrl(struct v4l2_ctrl *ctrl)
  750. {
  751. struct ov9282 *ov9282 =
  752. container_of_const(ctrl->handler, struct ov9282, ctrl_handler);
  753. if (ctrl->id == V4L2_CID_FLASH_DURATION) {
  754. u32 us = ctrl->val;
  755. u32 fd = ov9282_us_to_flash_duration(ov9282, us);
  756. /* get nearest strobe_duration value */
  757. u32 us0 = ov9282_flash_duration_to_us(ov9282, fd);
  758. u32 us1 = ov9282_flash_duration_to_us(ov9282, fd + 1);
  759. if (abs(us1 - us) < abs(us - us0))
  760. ctrl->val = us1;
  761. else
  762. ctrl->val = us0;
  763. if (us != ctrl->val)
  764. dev_dbg(ov9282->dev, "using next valid strobe_duration %u instead of %u\n",
  765. ctrl->val, us);
  766. }
  767. return 0;
  768. }
  769. /* V4l2 subdevice control ops*/
  770. static const struct v4l2_ctrl_ops ov9282_ctrl_ops = {
  771. .s_ctrl = ov9282_set_ctrl,
  772. .try_ctrl = ov9282_try_ctrl,
  773. };
  774. /**
  775. * ov9282_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
  776. * @sd: pointer to ov9282 V4L2 sub-device structure
  777. * @sd_state: V4L2 sub-device configuration
  778. * @code: V4L2 sub-device code enumeration need to be filled
  779. *
  780. * Return: 0 if successful, error code otherwise.
  781. */
  782. static int ov9282_enum_mbus_code(struct v4l2_subdev *sd,
  783. struct v4l2_subdev_state *sd_state,
  784. struct v4l2_subdev_mbus_code_enum *code)
  785. {
  786. switch (code->index) {
  787. case 0:
  788. code->code = MEDIA_BUS_FMT_Y10_1X10;
  789. break;
  790. case 1:
  791. code->code = MEDIA_BUS_FMT_Y8_1X8;
  792. break;
  793. default:
  794. return -EINVAL;
  795. }
  796. return 0;
  797. }
  798. /**
  799. * ov9282_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
  800. * @sd: pointer to ov9282 V4L2 sub-device structure
  801. * @sd_state: V4L2 sub-device configuration
  802. * @fsize: V4L2 sub-device size enumeration need to be filled
  803. *
  804. * Return: 0 if successful, error code otherwise.
  805. */
  806. static int ov9282_enum_frame_size(struct v4l2_subdev *sd,
  807. struct v4l2_subdev_state *sd_state,
  808. struct v4l2_subdev_frame_size_enum *fsize)
  809. {
  810. if (fsize->index >= ARRAY_SIZE(supported_modes))
  811. return -EINVAL;
  812. if (fsize->code != MEDIA_BUS_FMT_Y10_1X10 &&
  813. fsize->code != MEDIA_BUS_FMT_Y8_1X8)
  814. return -EINVAL;
  815. fsize->min_width = supported_modes[fsize->index].width;
  816. fsize->max_width = fsize->min_width;
  817. fsize->min_height = supported_modes[fsize->index].height;
  818. fsize->max_height = fsize->min_height;
  819. return 0;
  820. }
  821. /**
  822. * ov9282_fill_pad_format() - Fill subdevice pad format
  823. * from selected sensor mode
  824. * @ov9282: pointer to ov9282 device
  825. * @mode: pointer to ov9282_mode sensor mode
  826. * @code: mbus code to be stored
  827. * @fmt: V4L2 sub-device format need to be filled
  828. */
  829. static void ov9282_fill_pad_format(struct ov9282 *ov9282,
  830. const struct ov9282_mode *mode,
  831. u32 code,
  832. struct v4l2_subdev_format *fmt)
  833. {
  834. fmt->format.width = mode->width;
  835. fmt->format.height = mode->height;
  836. fmt->format.code = code;
  837. fmt->format.field = V4L2_FIELD_NONE;
  838. fmt->format.colorspace = V4L2_COLORSPACE_RAW;
  839. fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  840. fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
  841. fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
  842. }
  843. /**
  844. * ov9282_get_pad_format() - Get subdevice pad format
  845. * @sd: pointer to ov9282 V4L2 sub-device structure
  846. * @sd_state: V4L2 sub-device configuration
  847. * @fmt: V4L2 sub-device format need to be set
  848. *
  849. * Return: 0 if successful, error code otherwise.
  850. */
  851. static int ov9282_get_pad_format(struct v4l2_subdev *sd,
  852. struct v4l2_subdev_state *sd_state,
  853. struct v4l2_subdev_format *fmt)
  854. {
  855. struct ov9282 *ov9282 = to_ov9282(sd);
  856. mutex_lock(&ov9282->mutex);
  857. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  858. struct v4l2_mbus_framefmt *framefmt;
  859. framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
  860. fmt->format = *framefmt;
  861. } else {
  862. ov9282_fill_pad_format(ov9282, ov9282->cur_mode, ov9282->code,
  863. fmt);
  864. }
  865. mutex_unlock(&ov9282->mutex);
  866. return 0;
  867. }
  868. /**
  869. * ov9282_set_pad_format() - Set subdevice pad format
  870. * @sd: pointer to ov9282 V4L2 sub-device structure
  871. * @sd_state: V4L2 sub-device configuration
  872. * @fmt: V4L2 sub-device format need to be set
  873. *
  874. * Return: 0 if successful, error code otherwise.
  875. */
  876. static int ov9282_set_pad_format(struct v4l2_subdev *sd,
  877. struct v4l2_subdev_state *sd_state,
  878. struct v4l2_subdev_format *fmt)
  879. {
  880. struct ov9282 *ov9282 = to_ov9282(sd);
  881. const struct ov9282_mode *mode;
  882. u32 code;
  883. int ret = 0;
  884. mutex_lock(&ov9282->mutex);
  885. mode = v4l2_find_nearest_size(supported_modes,
  886. ARRAY_SIZE(supported_modes),
  887. width, height,
  888. fmt->format.width,
  889. fmt->format.height);
  890. if (fmt->format.code == MEDIA_BUS_FMT_Y8_1X8)
  891. code = MEDIA_BUS_FMT_Y8_1X8;
  892. else
  893. code = MEDIA_BUS_FMT_Y10_1X10;
  894. ov9282_fill_pad_format(ov9282, mode, code, fmt);
  895. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  896. struct v4l2_mbus_framefmt *framefmt;
  897. framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
  898. *framefmt = fmt->format;
  899. } else {
  900. ret = ov9282_update_controls(ov9282, mode, fmt);
  901. if (!ret) {
  902. ov9282->cur_mode = mode;
  903. ov9282->code = code;
  904. }
  905. }
  906. mutex_unlock(&ov9282->mutex);
  907. return ret;
  908. }
  909. /**
  910. * ov9282_init_state() - Initialize sub-device state
  911. * @sd: pointer to ov9282 V4L2 sub-device structure
  912. * @sd_state: V4L2 sub-device configuration
  913. *
  914. * Return: 0 if successful, error code otherwise.
  915. */
  916. static int ov9282_init_state(struct v4l2_subdev *sd,
  917. struct v4l2_subdev_state *sd_state)
  918. {
  919. struct ov9282 *ov9282 = to_ov9282(sd);
  920. struct v4l2_subdev_format fmt = { 0 };
  921. fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  922. ov9282_fill_pad_format(ov9282, &supported_modes[DEFAULT_MODE],
  923. ov9282->code, &fmt);
  924. return ov9282_set_pad_format(sd, sd_state, &fmt);
  925. }
  926. static const struct v4l2_rect *
  927. __ov9282_get_pad_crop(struct ov9282 *ov9282,
  928. struct v4l2_subdev_state *sd_state,
  929. unsigned int pad, enum v4l2_subdev_format_whence which)
  930. {
  931. switch (which) {
  932. case V4L2_SUBDEV_FORMAT_TRY:
  933. return v4l2_subdev_state_get_crop(sd_state, pad);
  934. case V4L2_SUBDEV_FORMAT_ACTIVE:
  935. return &ov9282->cur_mode->crop;
  936. }
  937. return NULL;
  938. }
  939. static int ov9282_get_selection(struct v4l2_subdev *sd,
  940. struct v4l2_subdev_state *sd_state,
  941. struct v4l2_subdev_selection *sel)
  942. {
  943. switch (sel->target) {
  944. case V4L2_SEL_TGT_CROP: {
  945. struct ov9282 *ov9282 = to_ov9282(sd);
  946. mutex_lock(&ov9282->mutex);
  947. sel->r = *__ov9282_get_pad_crop(ov9282, sd_state, sel->pad,
  948. sel->which);
  949. mutex_unlock(&ov9282->mutex);
  950. return 0;
  951. }
  952. case V4L2_SEL_TGT_NATIVE_SIZE:
  953. sel->r.top = 0;
  954. sel->r.left = 0;
  955. sel->r.width = OV9282_NATIVE_WIDTH;
  956. sel->r.height = OV9282_NATIVE_HEIGHT;
  957. return 0;
  958. case V4L2_SEL_TGT_CROP_DEFAULT:
  959. case V4L2_SEL_TGT_CROP_BOUNDS:
  960. sel->r.top = OV9282_PIXEL_ARRAY_TOP;
  961. sel->r.left = OV9282_PIXEL_ARRAY_LEFT;
  962. sel->r.width = OV9282_PIXEL_ARRAY_WIDTH;
  963. sel->r.height = OV9282_PIXEL_ARRAY_HEIGHT;
  964. return 0;
  965. }
  966. return -EINVAL;
  967. }
  968. /**
  969. * ov9282_start_streaming() - Start sensor stream
  970. * @ov9282: pointer to ov9282 device
  971. *
  972. * Return: 0 if successful, error code otherwise.
  973. */
  974. static int ov9282_start_streaming(struct ov9282 *ov9282)
  975. {
  976. const struct ov9282_reg bitdepth_regs[2][2] = {
  977. {
  978. {OV9282_REG_PLL_CTRL_0D, OV9282_PLL_CTRL_0D_RAW10},
  979. {OV9282_REG_ANA_CORE_2, OV9282_ANA_CORE2_RAW10},
  980. }, {
  981. {OV9282_REG_PLL_CTRL_0D, OV9282_PLL_CTRL_0D_RAW8},
  982. {OV9282_REG_ANA_CORE_2, OV9282_ANA_CORE2_RAW8},
  983. }
  984. };
  985. const struct ov9282_reg_list *reg_list;
  986. int bitdepth_index;
  987. int ret;
  988. /* Write common registers */
  989. ret = ov9282_write_regs(ov9282, common_regs_list.regs,
  990. common_regs_list.num_of_regs);
  991. if (ret) {
  992. dev_err(ov9282->dev, "fail to write common registers");
  993. return ret;
  994. }
  995. bitdepth_index = ov9282->code == MEDIA_BUS_FMT_Y10_1X10 ? 0 : 1;
  996. ret = ov9282_write_regs(ov9282, bitdepth_regs[bitdepth_index], 2);
  997. if (ret) {
  998. dev_err(ov9282->dev, "fail to write bitdepth regs");
  999. return ret;
  1000. }
  1001. /* Write sensor mode registers */
  1002. reg_list = &ov9282->cur_mode->reg_list;
  1003. ret = ov9282_write_regs(ov9282, reg_list->regs, reg_list->num_of_regs);
  1004. if (ret) {
  1005. dev_err(ov9282->dev, "fail to write initial registers");
  1006. return ret;
  1007. }
  1008. /* Setup handler will write actual exposure and gain */
  1009. ret = __v4l2_ctrl_handler_setup(ov9282->sd.ctrl_handler);
  1010. if (ret) {
  1011. dev_err(ov9282->dev, "fail to setup handler");
  1012. return ret;
  1013. }
  1014. /* Start streaming */
  1015. ret = ov9282_write_reg(ov9282, OV9282_REG_MODE_SELECT,
  1016. 1, OV9282_MODE_STREAMING);
  1017. if (ret) {
  1018. dev_err(ov9282->dev, "fail to start streaming");
  1019. return ret;
  1020. }
  1021. return 0;
  1022. }
  1023. /**
  1024. * ov9282_stop_streaming() - Stop sensor stream
  1025. * @ov9282: pointer to ov9282 device
  1026. *
  1027. * Return: 0 if successful, error code otherwise.
  1028. */
  1029. static int ov9282_stop_streaming(struct ov9282 *ov9282)
  1030. {
  1031. return ov9282_write_reg(ov9282, OV9282_REG_MODE_SELECT,
  1032. 1, OV9282_MODE_STANDBY);
  1033. }
  1034. /**
  1035. * ov9282_set_stream() - Enable sensor streaming
  1036. * @sd: pointer to ov9282 subdevice
  1037. * @enable: set to enable sensor streaming
  1038. *
  1039. * Return: 0 if successful, error code otherwise.
  1040. */
  1041. static int ov9282_set_stream(struct v4l2_subdev *sd, int enable)
  1042. {
  1043. struct ov9282 *ov9282 = to_ov9282(sd);
  1044. int ret;
  1045. mutex_lock(&ov9282->mutex);
  1046. if (enable) {
  1047. ret = pm_runtime_resume_and_get(ov9282->dev);
  1048. if (ret)
  1049. goto error_unlock;
  1050. ret = ov9282_start_streaming(ov9282);
  1051. if (ret)
  1052. goto error_power_off;
  1053. } else {
  1054. ov9282_stop_streaming(ov9282);
  1055. pm_runtime_put(ov9282->dev);
  1056. }
  1057. mutex_unlock(&ov9282->mutex);
  1058. return 0;
  1059. error_power_off:
  1060. pm_runtime_put(ov9282->dev);
  1061. error_unlock:
  1062. mutex_unlock(&ov9282->mutex);
  1063. return ret;
  1064. }
  1065. /**
  1066. * ov9282_detect() - Detect ov9282 sensor
  1067. * @ov9282: pointer to ov9282 device
  1068. *
  1069. * Return: 0 if successful, -EIO if sensor id does not match
  1070. */
  1071. static int ov9282_detect(struct ov9282 *ov9282)
  1072. {
  1073. int ret;
  1074. u32 val;
  1075. ret = ov9282_read_reg(ov9282, OV9282_REG_ID, 2, &val);
  1076. if (ret)
  1077. return ret;
  1078. if (val != OV9282_ID) {
  1079. dev_err(ov9282->dev, "chip id mismatch: %x!=%x",
  1080. OV9282_ID, val);
  1081. return -ENXIO;
  1082. }
  1083. return 0;
  1084. }
  1085. static int ov9282_configure_regulators(struct ov9282 *ov9282)
  1086. {
  1087. unsigned int i;
  1088. for (i = 0; i < OV9282_NUM_SUPPLIES; i++)
  1089. ov9282->supplies[i].supply = ov9282_supply_names[i];
  1090. return devm_regulator_bulk_get(ov9282->dev,
  1091. OV9282_NUM_SUPPLIES,
  1092. ov9282->supplies);
  1093. }
  1094. /**
  1095. * ov9282_parse_hw_config() - Parse HW configuration and check if supported
  1096. * @ov9282: pointer to ov9282 device
  1097. *
  1098. * Return: 0 if successful, error code otherwise.
  1099. */
  1100. static int ov9282_parse_hw_config(struct ov9282 *ov9282)
  1101. {
  1102. struct fwnode_handle *fwnode = dev_fwnode(ov9282->dev);
  1103. struct v4l2_fwnode_endpoint bus_cfg = {
  1104. .bus_type = V4L2_MBUS_CSI2_DPHY
  1105. };
  1106. struct fwnode_handle *ep;
  1107. unsigned long rate;
  1108. unsigned int i;
  1109. int ret;
  1110. if (!fwnode)
  1111. return -ENXIO;
  1112. /* Request optional reset pin */
  1113. ov9282->reset_gpio = devm_gpiod_get_optional(ov9282->dev, "reset",
  1114. GPIOD_OUT_LOW);
  1115. if (IS_ERR(ov9282->reset_gpio)) {
  1116. dev_err(ov9282->dev, "failed to get reset gpio %pe",
  1117. ov9282->reset_gpio);
  1118. return PTR_ERR(ov9282->reset_gpio);
  1119. }
  1120. /* Get sensor input clock */
  1121. ov9282->inclk = devm_v4l2_sensor_clk_get(ov9282->dev, NULL);
  1122. if (IS_ERR(ov9282->inclk))
  1123. return dev_err_probe(ov9282->dev, PTR_ERR(ov9282->inclk),
  1124. "could not get inclk\n");
  1125. ret = ov9282_configure_regulators(ov9282);
  1126. if (ret)
  1127. return dev_err_probe(ov9282->dev, ret,
  1128. "Failed to get power regulators\n");
  1129. rate = clk_get_rate(ov9282->inclk);
  1130. if (rate != OV9282_INCLK_RATE) {
  1131. dev_err(ov9282->dev, "inclk frequency mismatch");
  1132. return -EINVAL;
  1133. }
  1134. ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
  1135. if (!ep)
  1136. return -ENXIO;
  1137. ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
  1138. fwnode_handle_put(ep);
  1139. if (ret)
  1140. return ret;
  1141. ov9282->noncontinuous_clock =
  1142. bus_cfg.bus.mipi_csi2.flags & V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
  1143. if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV9282_NUM_DATA_LANES) {
  1144. dev_err(ov9282->dev,
  1145. "number of CSI2 data lanes %d is not supported",
  1146. bus_cfg.bus.mipi_csi2.num_data_lanes);
  1147. ret = -EINVAL;
  1148. goto done_endpoint_free;
  1149. }
  1150. if (!bus_cfg.nr_of_link_frequencies) {
  1151. dev_err(ov9282->dev, "no link frequencies defined");
  1152. ret = -EINVAL;
  1153. goto done_endpoint_free;
  1154. }
  1155. for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
  1156. if (bus_cfg.link_frequencies[i] == OV9282_LINK_FREQ)
  1157. goto done_endpoint_free;
  1158. ret = -EINVAL;
  1159. done_endpoint_free:
  1160. v4l2_fwnode_endpoint_free(&bus_cfg);
  1161. return ret;
  1162. }
  1163. /* V4l2 subdevice ops */
  1164. static const struct v4l2_subdev_core_ops ov9282_core_ops = {
  1165. .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  1166. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  1167. };
  1168. static const struct v4l2_subdev_video_ops ov9282_video_ops = {
  1169. .s_stream = ov9282_set_stream,
  1170. };
  1171. static const struct v4l2_subdev_pad_ops ov9282_pad_ops = {
  1172. .enum_mbus_code = ov9282_enum_mbus_code,
  1173. .enum_frame_size = ov9282_enum_frame_size,
  1174. .get_fmt = ov9282_get_pad_format,
  1175. .set_fmt = ov9282_set_pad_format,
  1176. .get_selection = ov9282_get_selection,
  1177. };
  1178. static const struct v4l2_subdev_ops ov9282_subdev_ops = {
  1179. .core = &ov9282_core_ops,
  1180. .video = &ov9282_video_ops,
  1181. .pad = &ov9282_pad_ops,
  1182. };
  1183. static const struct v4l2_subdev_internal_ops ov9282_internal_ops = {
  1184. .init_state = ov9282_init_state,
  1185. };
  1186. /**
  1187. * ov9282_power_on() - Sensor power on sequence
  1188. * @dev: pointer to i2c device
  1189. *
  1190. * Return: 0 if successful, error code otherwise.
  1191. */
  1192. static int ov9282_power_on(struct device *dev)
  1193. {
  1194. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  1195. struct ov9282 *ov9282 = to_ov9282(sd);
  1196. int ret;
  1197. ret = regulator_bulk_enable(OV9282_NUM_SUPPLIES, ov9282->supplies);
  1198. if (ret < 0) {
  1199. dev_err(dev, "Failed to enable regulators\n");
  1200. return ret;
  1201. }
  1202. usleep_range(400, 600);
  1203. gpiod_set_value_cansleep(ov9282->reset_gpio, 1);
  1204. ret = clk_prepare_enable(ov9282->inclk);
  1205. if (ret) {
  1206. dev_err(ov9282->dev, "fail to enable inclk");
  1207. goto error_reset;
  1208. }
  1209. usleep_range(400, 600);
  1210. ret = ov9282_write_reg(ov9282, OV9282_REG_MIPI_CTRL00, 1,
  1211. ov9282->noncontinuous_clock ?
  1212. OV9282_GATED_CLOCK : 0);
  1213. if (ret) {
  1214. dev_err(ov9282->dev, "fail to write MIPI_CTRL00");
  1215. goto error_clk;
  1216. }
  1217. return 0;
  1218. error_clk:
  1219. clk_disable_unprepare(ov9282->inclk);
  1220. error_reset:
  1221. gpiod_set_value_cansleep(ov9282->reset_gpio, 0);
  1222. regulator_bulk_disable(OV9282_NUM_SUPPLIES, ov9282->supplies);
  1223. return ret;
  1224. }
  1225. /**
  1226. * ov9282_power_off() - Sensor power off sequence
  1227. * @dev: pointer to i2c device
  1228. *
  1229. * Return: 0 if successful, error code otherwise.
  1230. */
  1231. static int ov9282_power_off(struct device *dev)
  1232. {
  1233. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  1234. struct ov9282 *ov9282 = to_ov9282(sd);
  1235. gpiod_set_value_cansleep(ov9282->reset_gpio, 0);
  1236. clk_disable_unprepare(ov9282->inclk);
  1237. regulator_bulk_disable(OV9282_NUM_SUPPLIES, ov9282->supplies);
  1238. return 0;
  1239. }
  1240. /**
  1241. * ov9282_init_controls() - Initialize sensor subdevice controls
  1242. * @ov9282: pointer to ov9282 device
  1243. *
  1244. * Return: 0 if successful, error code otherwise.
  1245. */
  1246. static int ov9282_init_controls(struct ov9282 *ov9282)
  1247. {
  1248. struct v4l2_ctrl_handler *ctrl_hdlr = &ov9282->ctrl_handler;
  1249. const struct ov9282_mode *mode = ov9282->cur_mode;
  1250. struct v4l2_fwnode_device_properties props;
  1251. u32 hblank_min;
  1252. u32 exposure_us;
  1253. u32 lpfr;
  1254. int ret;
  1255. ret = v4l2_ctrl_handler_init(ctrl_hdlr, 12);
  1256. if (ret)
  1257. return ret;
  1258. /* Serialize controls with sensor device */
  1259. ctrl_hdlr->lock = &ov9282->mutex;
  1260. /* Initialize exposure and gain */
  1261. lpfr = mode->vblank + mode->height;
  1262. ov9282->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  1263. &ov9282_ctrl_ops,
  1264. V4L2_CID_EXPOSURE,
  1265. OV9282_EXPOSURE_MIN,
  1266. lpfr - OV9282_EXPOSURE_OFFSET,
  1267. OV9282_EXPOSURE_STEP,
  1268. OV9282_EXPOSURE_DEFAULT);
  1269. ov9282->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  1270. &ov9282_ctrl_ops,
  1271. V4L2_CID_ANALOGUE_GAIN,
  1272. OV9282_AGAIN_MIN,
  1273. OV9282_AGAIN_MAX,
  1274. OV9282_AGAIN_STEP,
  1275. OV9282_AGAIN_DEFAULT);
  1276. v4l2_ctrl_cluster(2, &ov9282->exp_ctrl);
  1277. ov9282->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  1278. &ov9282_ctrl_ops,
  1279. V4L2_CID_VBLANK,
  1280. mode->vblank_min,
  1281. mode->vblank_max,
  1282. 1, mode->vblank);
  1283. v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops, V4L2_CID_VFLIP,
  1284. 0, 1, 1, 1);
  1285. v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops, V4L2_CID_HFLIP,
  1286. 0, 1, 1, 1);
  1287. /* Read only controls */
  1288. ov9282->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops,
  1289. V4L2_CID_PIXEL_RATE,
  1290. OV9282_PIXEL_RATE_10BIT,
  1291. OV9282_PIXEL_RATE_10BIT, 1,
  1292. OV9282_PIXEL_RATE_10BIT);
  1293. ov9282->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
  1294. &ov9282_ctrl_ops,
  1295. V4L2_CID_LINK_FREQ,
  1296. ARRAY_SIZE(link_freq) -
  1297. 1,
  1298. mode->link_freq_idx,
  1299. link_freq);
  1300. if (ov9282->link_freq_ctrl)
  1301. ov9282->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1302. hblank_min = mode->hblank_min[ov9282->noncontinuous_clock ? 0 : 1];
  1303. ov9282->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  1304. &ov9282_ctrl_ops,
  1305. V4L2_CID_HBLANK,
  1306. hblank_min,
  1307. OV9282_TIMING_HTS_MAX - mode->width,
  1308. 1, hblank_min);
  1309. /* Flash/Strobe controls */
  1310. v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops,
  1311. V4L2_CID_FLASH_STROBE_OE, 0, 1, 1, 0);
  1312. exposure_us = ov9282_exposure_to_us(ov9282, OV9282_EXPOSURE_DEFAULT);
  1313. ov9282->flash_duration =
  1314. v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops,
  1315. V4L2_CID_FLASH_DURATION, 0, exposure_us, 1,
  1316. OV9282_STROBE_FRAME_SPAN_DEFAULT);
  1317. ret = v4l2_fwnode_device_parse(ov9282->dev, &props);
  1318. if (!ret) {
  1319. /* Failure sets ctrl_hdlr->error, which we check afterwards anyway */
  1320. v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov9282_ctrl_ops,
  1321. &props);
  1322. }
  1323. if (ctrl_hdlr->error || ret) {
  1324. dev_err(ov9282->dev, "control init failed: %d",
  1325. ctrl_hdlr->error);
  1326. v4l2_ctrl_handler_free(ctrl_hdlr);
  1327. return ctrl_hdlr->error;
  1328. }
  1329. ov9282->sd.ctrl_handler = ctrl_hdlr;
  1330. return 0;
  1331. }
  1332. /**
  1333. * ov9282_probe() - I2C client device binding
  1334. * @client: pointer to i2c client device
  1335. *
  1336. * Return: 0 if successful, error code otherwise.
  1337. */
  1338. static int ov9282_probe(struct i2c_client *client)
  1339. {
  1340. struct ov9282 *ov9282;
  1341. int ret;
  1342. ov9282 = devm_kzalloc(&client->dev, sizeof(*ov9282), GFP_KERNEL);
  1343. if (!ov9282)
  1344. return -ENOMEM;
  1345. ov9282->dev = &client->dev;
  1346. /* Initialize subdev */
  1347. v4l2_i2c_subdev_init(&ov9282->sd, client, &ov9282_subdev_ops);
  1348. ov9282->sd.internal_ops = &ov9282_internal_ops;
  1349. v4l2_i2c_subdev_set_name(&ov9282->sd, client,
  1350. device_get_match_data(ov9282->dev), NULL);
  1351. ret = ov9282_parse_hw_config(ov9282);
  1352. if (ret) {
  1353. dev_err(ov9282->dev, "HW configuration is not supported");
  1354. return ret;
  1355. }
  1356. mutex_init(&ov9282->mutex);
  1357. ret = ov9282_power_on(ov9282->dev);
  1358. if (ret) {
  1359. dev_err(ov9282->dev, "failed to power-on the sensor");
  1360. goto error_mutex_destroy;
  1361. }
  1362. /* Check module identity */
  1363. ret = ov9282_detect(ov9282);
  1364. if (ret) {
  1365. dev_err(ov9282->dev, "failed to find sensor: %d", ret);
  1366. goto error_power_off;
  1367. }
  1368. /* Set default mode to first mode */
  1369. ov9282->cur_mode = &supported_modes[DEFAULT_MODE];
  1370. ov9282->code = MEDIA_BUS_FMT_Y10_1X10;
  1371. ov9282->vblank = ov9282->cur_mode->vblank;
  1372. ret = ov9282_init_controls(ov9282);
  1373. if (ret) {
  1374. dev_err(ov9282->dev, "failed to init controls: %d", ret);
  1375. goto error_power_off;
  1376. }
  1377. /* Initialize subdev */
  1378. ov9282->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
  1379. V4L2_SUBDEV_FL_HAS_EVENTS;
  1380. ov9282->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1381. /* Initialize source pad */
  1382. ov9282->pad.flags = MEDIA_PAD_FL_SOURCE;
  1383. ret = media_entity_pads_init(&ov9282->sd.entity, 1, &ov9282->pad);
  1384. if (ret) {
  1385. dev_err(ov9282->dev, "failed to init entity pads: %d", ret);
  1386. goto error_handler_free;
  1387. }
  1388. ret = v4l2_async_register_subdev_sensor(&ov9282->sd);
  1389. if (ret < 0) {
  1390. dev_err(ov9282->dev,
  1391. "failed to register async subdev: %d", ret);
  1392. goto error_media_entity;
  1393. }
  1394. pm_runtime_set_active(ov9282->dev);
  1395. pm_runtime_enable(ov9282->dev);
  1396. pm_runtime_idle(ov9282->dev);
  1397. return 0;
  1398. error_media_entity:
  1399. media_entity_cleanup(&ov9282->sd.entity);
  1400. error_handler_free:
  1401. v4l2_ctrl_handler_free(ov9282->sd.ctrl_handler);
  1402. error_power_off:
  1403. ov9282_power_off(ov9282->dev);
  1404. error_mutex_destroy:
  1405. mutex_destroy(&ov9282->mutex);
  1406. return ret;
  1407. }
  1408. /**
  1409. * ov9282_remove() - I2C client device unbinding
  1410. * @client: pointer to I2C client device
  1411. *
  1412. * Return: 0 if successful, error code otherwise.
  1413. */
  1414. static void ov9282_remove(struct i2c_client *client)
  1415. {
  1416. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1417. struct ov9282 *ov9282 = to_ov9282(sd);
  1418. v4l2_async_unregister_subdev(sd);
  1419. media_entity_cleanup(&sd->entity);
  1420. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1421. pm_runtime_disable(&client->dev);
  1422. if (!pm_runtime_status_suspended(&client->dev))
  1423. ov9282_power_off(&client->dev);
  1424. pm_runtime_set_suspended(&client->dev);
  1425. mutex_destroy(&ov9282->mutex);
  1426. }
  1427. static const struct dev_pm_ops ov9282_pm_ops = {
  1428. SET_RUNTIME_PM_OPS(ov9282_power_off, ov9282_power_on, NULL)
  1429. };
  1430. static const struct of_device_id ov9282_of_match[] = {
  1431. { .compatible = "ovti,ov9281", .data = "ov9281" },
  1432. { .compatible = "ovti,ov9282", .data = "ov9282" },
  1433. { }
  1434. };
  1435. MODULE_DEVICE_TABLE(of, ov9282_of_match);
  1436. static struct i2c_driver ov9282_driver = {
  1437. .probe = ov9282_probe,
  1438. .remove = ov9282_remove,
  1439. .driver = {
  1440. .name = "ov9282",
  1441. .pm = &ov9282_pm_ops,
  1442. .of_match_table = ov9282_of_match,
  1443. },
  1444. };
  1445. module_i2c_driver(ov9282_driver);
  1446. MODULE_DESCRIPTION("OmniVision ov9282 sensor driver");
  1447. MODULE_LICENSE("GPL");