s5k5baf.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054
  1. /*
  2. * Driver for Samsung S5K5BAF UXGA 1/5" 2M CMOS Image Sensor
  3. * with embedded SoC ISP.
  4. *
  5. * Copyright (C) 2013, Samsung Electronics Co., Ltd.
  6. * Andrzej Hajda <a.hajda@samsung.com>
  7. *
  8. * Based on S5K6AA driver authored by Sylwester Nawrocki
  9. * Copyright (C) 2013, Samsung Electronics Co., Ltd.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/firmware.h>
  18. #include <linux/gpio.h>
  19. #include <linux/i2c.h>
  20. #include <linux/media.h>
  21. #include <linux/module.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/of_graph.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/slab.h>
  26. #include <media/media-entity.h>
  27. #include <media/v4l2-ctrls.h>
  28. #include <media/v4l2-device.h>
  29. #include <media/v4l2-subdev.h>
  30. #include <media/v4l2-mediabus.h>
  31. #include <media/v4l2-of.h>
  32. static int debug;
  33. module_param(debug, int, 0644);
  34. #define S5K5BAF_DRIVER_NAME "s5k5baf"
  35. #define S5K5BAF_DEFAULT_MCLK_FREQ 24000000U
  36. #define S5K5BAF_CLK_NAME "mclk"
  37. #define S5K5BAF_FW_FILENAME "s5k5baf-cfg.bin"
  38. #define S5K5BAF_FW_TAG "SF00"
  39. #define S5K5BAG_FW_TAG_LEN 2
  40. #define S5K5BAG_FW_MAX_COUNT 16
  41. #define S5K5BAF_CIS_WIDTH 1600
  42. #define S5K5BAF_CIS_HEIGHT 1200
  43. #define S5K5BAF_WIN_WIDTH_MIN 8
  44. #define S5K5BAF_WIN_HEIGHT_MIN 8
  45. #define S5K5BAF_GAIN_RED_DEF 127
  46. #define S5K5BAF_GAIN_GREEN_DEF 95
  47. #define S5K5BAF_GAIN_BLUE_DEF 180
  48. /* Default number of MIPI CSI-2 data lanes used */
  49. #define S5K5BAF_DEF_NUM_LANES 1
  50. #define AHB_MSB_ADDR_PTR 0xfcfc
  51. /*
  52. * Register interface pages (the most significant word of the address)
  53. */
  54. #define PAGE_IF_HW 0xd000
  55. #define PAGE_IF_SW 0x7000
  56. /*
  57. * H/W register Interface (PAGE_IF_HW)
  58. */
  59. #define REG_SW_LOAD_COMPLETE 0x0014
  60. #define REG_CMDWR_PAGE 0x0028
  61. #define REG_CMDWR_ADDR 0x002a
  62. #define REG_CMDRD_PAGE 0x002c
  63. #define REG_CMDRD_ADDR 0x002e
  64. #define REG_CMD_BUF 0x0f12
  65. #define REG_SET_HOST_INT 0x1000
  66. #define REG_CLEAR_HOST_INT 0x1030
  67. #define REG_PATTERN_SET 0x3100
  68. #define REG_PATTERN_WIDTH 0x3118
  69. #define REG_PATTERN_HEIGHT 0x311a
  70. #define REG_PATTERN_PARAM 0x311c
  71. /*
  72. * S/W register interface (PAGE_IF_SW)
  73. */
  74. /* Firmware revision information */
  75. #define REG_FW_APIVER 0x012e
  76. #define S5K5BAF_FW_APIVER 0x0001
  77. #define REG_FW_REVISION 0x0130
  78. #define REG_FW_SENSOR_ID 0x0152
  79. /* Initialization parameters */
  80. /* Master clock frequency in KHz */
  81. #define REG_I_INCLK_FREQ_L 0x01b8
  82. #define REG_I_INCLK_FREQ_H 0x01ba
  83. #define MIN_MCLK_FREQ_KHZ 6000U
  84. #define MAX_MCLK_FREQ_KHZ 48000U
  85. #define REG_I_USE_NPVI_CLOCKS 0x01c6
  86. #define NPVI_CLOCKS 1
  87. #define REG_I_USE_NMIPI_CLOCKS 0x01c8
  88. #define NMIPI_CLOCKS 1
  89. #define REG_I_BLOCK_INTERNAL_PLL_CALC 0x01ca
  90. /* Clock configurations, n = 0..2. REG_I_* frequency unit is 4 kHz. */
  91. #define REG_I_OPCLK_4KHZ(n) ((n) * 6 + 0x01cc)
  92. #define REG_I_MIN_OUTRATE_4KHZ(n) ((n) * 6 + 0x01ce)
  93. #define REG_I_MAX_OUTRATE_4KHZ(n) ((n) * 6 + 0x01d0)
  94. #define SCLK_PVI_FREQ 24000
  95. #define SCLK_MIPI_FREQ 48000
  96. #define PCLK_MIN_FREQ 6000
  97. #define PCLK_MAX_FREQ 48000
  98. #define REG_I_USE_REGS_API 0x01de
  99. #define REG_I_INIT_PARAMS_UPDATED 0x01e0
  100. #define REG_I_ERROR_INFO 0x01e2
  101. /* General purpose parameters */
  102. #define REG_USER_BRIGHTNESS 0x01e4
  103. #define REG_USER_CONTRAST 0x01e6
  104. #define REG_USER_SATURATION 0x01e8
  105. #define REG_USER_SHARPBLUR 0x01ea
  106. #define REG_G_SPEC_EFFECTS 0x01ee
  107. #define REG_G_ENABLE_PREV 0x01f0
  108. #define REG_G_ENABLE_PREV_CHG 0x01f2
  109. #define REG_G_NEW_CFG_SYNC 0x01f8
  110. #define REG_G_PREVREQ_IN_WIDTH 0x01fa
  111. #define REG_G_PREVREQ_IN_HEIGHT 0x01fc
  112. #define REG_G_PREVREQ_IN_XOFFS 0x01fe
  113. #define REG_G_PREVREQ_IN_YOFFS 0x0200
  114. #define REG_G_PREVZOOM_IN_WIDTH 0x020a
  115. #define REG_G_PREVZOOM_IN_HEIGHT 0x020c
  116. #define REG_G_PREVZOOM_IN_XOFFS 0x020e
  117. #define REG_G_PREVZOOM_IN_YOFFS 0x0210
  118. #define REG_G_INPUTS_CHANGE_REQ 0x021a
  119. #define REG_G_ACTIVE_PREV_CFG 0x021c
  120. #define REG_G_PREV_CFG_CHG 0x021e
  121. #define REG_G_PREV_OPEN_AFTER_CH 0x0220
  122. #define REG_G_PREV_CFG_ERROR 0x0222
  123. #define CFG_ERROR_RANGE 0x0b
  124. #define REG_G_PREV_CFG_BYPASS_CHANGED 0x022a
  125. #define REG_G_ACTUAL_P_FR_TIME 0x023a
  126. #define REG_G_ACTUAL_P_OUT_RATE 0x023c
  127. #define REG_G_ACTUAL_C_FR_TIME 0x023e
  128. #define REG_G_ACTUAL_C_OUT_RATE 0x0240
  129. /* Preview control section. n = 0...4. */
  130. #define PREG(n, x) ((n) * 0x26 + x)
  131. #define REG_P_OUT_WIDTH(n) PREG(n, 0x0242)
  132. #define REG_P_OUT_HEIGHT(n) PREG(n, 0x0244)
  133. #define REG_P_FMT(n) PREG(n, 0x0246)
  134. #define REG_P_MAX_OUT_RATE(n) PREG(n, 0x0248)
  135. #define REG_P_MIN_OUT_RATE(n) PREG(n, 0x024a)
  136. #define REG_P_PVI_MASK(n) PREG(n, 0x024c)
  137. #define PVI_MASK_MIPI 0x52
  138. #define REG_P_CLK_INDEX(n) PREG(n, 0x024e)
  139. #define CLK_PVI_INDEX 0
  140. #define CLK_MIPI_INDEX NPVI_CLOCKS
  141. #define REG_P_FR_RATE_TYPE(n) PREG(n, 0x0250)
  142. #define FR_RATE_DYNAMIC 0
  143. #define FR_RATE_FIXED 1
  144. #define FR_RATE_FIXED_ACCURATE 2
  145. #define REG_P_FR_RATE_Q_TYPE(n) PREG(n, 0x0252)
  146. #define FR_RATE_Q_DYNAMIC 0
  147. #define FR_RATE_Q_BEST_FRRATE 1 /* Binning enabled */
  148. #define FR_RATE_Q_BEST_QUALITY 2 /* Binning disabled */
  149. /* Frame period in 0.1 ms units */
  150. #define REG_P_MAX_FR_TIME(n) PREG(n, 0x0254)
  151. #define REG_P_MIN_FR_TIME(n) PREG(n, 0x0256)
  152. #define S5K5BAF_MIN_FR_TIME 333 /* x100 us */
  153. #define S5K5BAF_MAX_FR_TIME 6500 /* x100 us */
  154. /* The below 5 registers are for "device correction" values */
  155. #define REG_P_SATURATION(n) PREG(n, 0x0258)
  156. #define REG_P_SHARP_BLUR(n) PREG(n, 0x025a)
  157. #define REG_P_GLAMOUR(n) PREG(n, 0x025c)
  158. #define REG_P_COLORTEMP(n) PREG(n, 0x025e)
  159. #define REG_P_GAMMA_INDEX(n) PREG(n, 0x0260)
  160. #define REG_P_PREV_MIRROR(n) PREG(n, 0x0262)
  161. #define REG_P_CAP_MIRROR(n) PREG(n, 0x0264)
  162. #define REG_P_CAP_ROTATION(n) PREG(n, 0x0266)
  163. /* Extended image property controls */
  164. /* Exposure time in 10 us units */
  165. #define REG_SF_USR_EXPOSURE_L 0x03bc
  166. #define REG_SF_USR_EXPOSURE_H 0x03be
  167. #define REG_SF_USR_EXPOSURE_CHG 0x03c0
  168. #define REG_SF_USR_TOT_GAIN 0x03c2
  169. #define REG_SF_USR_TOT_GAIN_CHG 0x03c4
  170. #define REG_SF_RGAIN 0x03c6
  171. #define REG_SF_RGAIN_CHG 0x03c8
  172. #define REG_SF_GGAIN 0x03ca
  173. #define REG_SF_GGAIN_CHG 0x03cc
  174. #define REG_SF_BGAIN 0x03ce
  175. #define REG_SF_BGAIN_CHG 0x03d0
  176. #define REG_SF_WBGAIN_CHG 0x03d2
  177. #define REG_SF_FLICKER_QUANT 0x03d4
  178. #define REG_SF_FLICKER_QUANT_CHG 0x03d6
  179. /* Output interface (parallel/MIPI) setup */
  180. #define REG_OIF_EN_MIPI_LANES 0x03f2
  181. #define REG_OIF_EN_PACKETS 0x03f4
  182. #define EN_PACKETS_CSI2 0xc3
  183. #define REG_OIF_CFG_CHG 0x03f6
  184. /* Auto-algorithms enable mask */
  185. #define REG_DBG_AUTOALG_EN 0x03f8
  186. #define AALG_ALL_EN BIT(0)
  187. #define AALG_AE_EN BIT(1)
  188. #define AALG_DIVLEI_EN BIT(2)
  189. #define AALG_WB_EN BIT(3)
  190. #define AALG_USE_WB_FOR_ISP BIT(4)
  191. #define AALG_FLICKER_EN BIT(5)
  192. #define AALG_FIT_EN BIT(6)
  193. #define AALG_WRHW_EN BIT(7)
  194. /* Pointers to color correction matrices */
  195. #define REG_PTR_CCM_HORIZON 0x06d0
  196. #define REG_PTR_CCM_INCANDESCENT 0x06d4
  197. #define REG_PTR_CCM_WARM_WHITE 0x06d8
  198. #define REG_PTR_CCM_COOL_WHITE 0x06dc
  199. #define REG_PTR_CCM_DL50 0x06e0
  200. #define REG_PTR_CCM_DL65 0x06e4
  201. #define REG_PTR_CCM_OUTDOOR 0x06ec
  202. #define REG_ARR_CCM(n) (0x2800 + 36 * (n))
  203. static const char * const s5k5baf_supply_names[] = {
  204. "vdda", /* Analog power supply 2.8V (2.6V to 3.0V) */
  205. "vddreg", /* Regulator input power supply 1.8V (1.7V to 1.9V)
  206. or 2.8V (2.6V to 3.0) */
  207. "vddio", /* I/O power supply 1.8V (1.65V to 1.95V)
  208. or 2.8V (2.5V to 3.1V) */
  209. };
  210. #define S5K5BAF_NUM_SUPPLIES ARRAY_SIZE(s5k5baf_supply_names)
  211. struct s5k5baf_gpio {
  212. int gpio;
  213. int level;
  214. };
  215. enum s5k5baf_gpio_id {
  216. STBY,
  217. RST,
  218. NUM_GPIOS,
  219. };
  220. #define PAD_CIS 0
  221. #define PAD_OUT 1
  222. #define NUM_CIS_PADS 1
  223. #define NUM_ISP_PADS 2
  224. struct s5k5baf_pixfmt {
  225. enum v4l2_mbus_pixelcode code;
  226. u32 colorspace;
  227. /* REG_P_FMT(x) register value */
  228. u16 reg_p_fmt;
  229. };
  230. struct s5k5baf_ctrls {
  231. struct v4l2_ctrl_handler handler;
  232. struct { /* Auto / manual white balance cluster */
  233. struct v4l2_ctrl *awb;
  234. struct v4l2_ctrl *gain_red;
  235. struct v4l2_ctrl *gain_blue;
  236. };
  237. struct { /* Mirror cluster */
  238. struct v4l2_ctrl *hflip;
  239. struct v4l2_ctrl *vflip;
  240. };
  241. struct { /* Auto exposure / manual exposure and gain cluster */
  242. struct v4l2_ctrl *auto_exp;
  243. struct v4l2_ctrl *exposure;
  244. struct v4l2_ctrl *gain;
  245. };
  246. };
  247. enum {
  248. S5K5BAF_FW_ID_PATCH,
  249. S5K5BAF_FW_ID_CCM,
  250. S5K5BAF_FW_ID_CIS,
  251. };
  252. struct s5k5baf_fw {
  253. u16 count;
  254. struct {
  255. u16 id;
  256. u16 offset;
  257. } seq[0];
  258. u16 data[0];
  259. };
  260. struct s5k5baf {
  261. struct s5k5baf_gpio gpios[NUM_GPIOS];
  262. enum v4l2_mbus_type bus_type;
  263. u8 nlanes;
  264. struct regulator_bulk_data supplies[S5K5BAF_NUM_SUPPLIES];
  265. struct clk *clock;
  266. u32 mclk_frequency;
  267. struct s5k5baf_fw *fw;
  268. struct v4l2_subdev cis_sd;
  269. struct media_pad cis_pad;
  270. struct v4l2_subdev sd;
  271. struct media_pad pads[NUM_ISP_PADS];
  272. /* protects the struct members below */
  273. struct mutex lock;
  274. int error;
  275. struct v4l2_rect crop_sink;
  276. struct v4l2_rect compose;
  277. struct v4l2_rect crop_source;
  278. /* index to s5k5baf_formats array */
  279. int pixfmt;
  280. /* actual frame interval in 100us */
  281. u16 fiv;
  282. /* requested frame interval in 100us */
  283. u16 req_fiv;
  284. /* cache for REG_DBG_AUTOALG_EN register */
  285. u16 auto_alg;
  286. struct s5k5baf_ctrls ctrls;
  287. unsigned int streaming:1;
  288. unsigned int apply_cfg:1;
  289. unsigned int apply_crop:1;
  290. unsigned int valid_auto_alg:1;
  291. unsigned int power;
  292. };
  293. static const struct s5k5baf_pixfmt s5k5baf_formats[] = {
  294. { V4L2_MBUS_FMT_VYUY8_2X8, V4L2_COLORSPACE_JPEG, 5 },
  295. /* range 16-240 */
  296. { V4L2_MBUS_FMT_VYUY8_2X8, V4L2_COLORSPACE_REC709, 6 },
  297. { V4L2_MBUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_JPEG, 0 },
  298. };
  299. static struct v4l2_rect s5k5baf_cis_rect = {
  300. 0, 0, S5K5BAF_CIS_WIDTH, S5K5BAF_CIS_HEIGHT
  301. };
  302. /* Setfile contains set of I2C command sequences. Each sequence has its ID.
  303. * setfile format:
  304. * u8 magic[4];
  305. * u16 count; number of sequences
  306. * struct {
  307. * u16 id; sequence id
  308. * u16 offset; sequence offset in data array
  309. * } seq[count];
  310. * u16 data[*]; array containing sequences
  311. *
  312. */
  313. static int s5k5baf_fw_parse(struct device *dev, struct s5k5baf_fw **fw,
  314. size_t count, const u16 *data)
  315. {
  316. struct s5k5baf_fw *f;
  317. u16 *d, i, *end;
  318. int ret;
  319. if (count < S5K5BAG_FW_TAG_LEN + 1) {
  320. dev_err(dev, "firmware file too short (%zu)\n", count);
  321. return -EINVAL;
  322. }
  323. ret = memcmp(data, S5K5BAF_FW_TAG, S5K5BAG_FW_TAG_LEN * sizeof(u16));
  324. if (ret != 0) {
  325. dev_err(dev, "invalid firmware magic number\n");
  326. return -EINVAL;
  327. }
  328. data += S5K5BAG_FW_TAG_LEN;
  329. count -= S5K5BAG_FW_TAG_LEN;
  330. d = devm_kzalloc(dev, count * sizeof(u16), GFP_KERNEL);
  331. for (i = 0; i < count; ++i)
  332. d[i] = le16_to_cpu(data[i]);
  333. f = (struct s5k5baf_fw *)d;
  334. if (count < 1 + 2 * f->count) {
  335. dev_err(dev, "invalid firmware header (count=%d size=%zu)\n",
  336. f->count, 2 * (count + S5K5BAG_FW_TAG_LEN));
  337. return -EINVAL;
  338. }
  339. end = d + count;
  340. d += 1 + 2 * f->count;
  341. for (i = 0; i < f->count; ++i) {
  342. if (f->seq[i].offset + d <= end)
  343. continue;
  344. dev_err(dev, "invalid firmware header (seq=%d)\n", i);
  345. return -EINVAL;
  346. }
  347. *fw = f;
  348. return 0;
  349. }
  350. static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
  351. {
  352. return &container_of(ctrl->handler, struct s5k5baf, ctrls.handler)->sd;
  353. }
  354. static inline bool s5k5baf_is_cis_subdev(struct v4l2_subdev *sd)
  355. {
  356. return sd->entity.type == MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
  357. }
  358. static inline struct s5k5baf *to_s5k5baf(struct v4l2_subdev *sd)
  359. {
  360. if (s5k5baf_is_cis_subdev(sd))
  361. return container_of(sd, struct s5k5baf, cis_sd);
  362. else
  363. return container_of(sd, struct s5k5baf, sd);
  364. }
  365. static u16 s5k5baf_i2c_read(struct s5k5baf *state, u16 addr)
  366. {
  367. struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
  368. __be16 w, r;
  369. struct i2c_msg msg[] = {
  370. { .addr = c->addr, .flags = 0,
  371. .len = 2, .buf = (u8 *)&w },
  372. { .addr = c->addr, .flags = I2C_M_RD,
  373. .len = 2, .buf = (u8 *)&r },
  374. };
  375. int ret;
  376. if (state->error)
  377. return 0;
  378. w = cpu_to_be16(addr);
  379. ret = i2c_transfer(c->adapter, msg, 2);
  380. r = be16_to_cpu(r);
  381. v4l2_dbg(3, debug, c, "i2c_read: 0x%04x : 0x%04x\n", addr, r);
  382. if (ret != 2) {
  383. v4l2_err(c, "i2c_read: error during transfer (%d)\n", ret);
  384. state->error = ret;
  385. }
  386. return r;
  387. }
  388. static void s5k5baf_i2c_write(struct s5k5baf *state, u16 addr, u16 val)
  389. {
  390. u8 buf[4] = { addr >> 8, addr & 0xFF, val >> 8, val & 0xFF };
  391. struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
  392. int ret;
  393. if (state->error)
  394. return;
  395. ret = i2c_master_send(c, buf, 4);
  396. v4l2_dbg(3, debug, c, "i2c_write: 0x%04x : 0x%04x\n", addr, val);
  397. if (ret != 4) {
  398. v4l2_err(c, "i2c_write: error during transfer (%d)\n", ret);
  399. state->error = ret;
  400. }
  401. }
  402. static u16 s5k5baf_read(struct s5k5baf *state, u16 addr)
  403. {
  404. s5k5baf_i2c_write(state, REG_CMDRD_ADDR, addr);
  405. return s5k5baf_i2c_read(state, REG_CMD_BUF);
  406. }
  407. static void s5k5baf_write(struct s5k5baf *state, u16 addr, u16 val)
  408. {
  409. s5k5baf_i2c_write(state, REG_CMDWR_ADDR, addr);
  410. s5k5baf_i2c_write(state, REG_CMD_BUF, val);
  411. }
  412. static void s5k5baf_write_arr_seq(struct s5k5baf *state, u16 addr,
  413. u16 count, const u16 *seq)
  414. {
  415. struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
  416. __be16 buf[65];
  417. s5k5baf_i2c_write(state, REG_CMDWR_ADDR, addr);
  418. if (state->error)
  419. return;
  420. v4l2_dbg(3, debug, c, "i2c_write_seq(count=%d): %*ph\n", count,
  421. min(2 * count, 64), seq);
  422. buf[0] = __constant_cpu_to_be16(REG_CMD_BUF);
  423. while (count > 0) {
  424. int n = min_t(int, count, ARRAY_SIZE(buf) - 1);
  425. int ret, i;
  426. for (i = 1; i <= n; ++i)
  427. buf[i] = cpu_to_be16(*seq++);
  428. i *= 2;
  429. ret = i2c_master_send(c, (char *)buf, i);
  430. if (ret != i) {
  431. v4l2_err(c, "i2c_write_seq: error during transfer (%d)\n", ret);
  432. state->error = ret;
  433. break;
  434. }
  435. count -= n;
  436. }
  437. }
  438. #define s5k5baf_write_seq(state, addr, seq...) \
  439. s5k5baf_write_arr_seq(state, addr, sizeof((char[]){ seq }), \
  440. (const u16 []){ seq });
  441. /* add items count at the beginning of the list */
  442. #define NSEQ(seq...) sizeof((char[]){ seq }), seq
  443. /*
  444. * s5k5baf_write_nseq() - Writes sequences of values to sensor memory via i2c
  445. * @nseq: sequence of u16 words in format:
  446. * (N, address, value[1]...value[N-1])*,0
  447. * Ex.:
  448. * u16 seq[] = { NSEQ(0x4000, 1, 1), NSEQ(0x4010, 640, 480), 0 };
  449. * ret = s5k5baf_write_nseq(c, seq);
  450. */
  451. static void s5k5baf_write_nseq(struct s5k5baf *state, const u16 *nseq)
  452. {
  453. int count;
  454. while ((count = *nseq++)) {
  455. u16 addr = *nseq++;
  456. --count;
  457. s5k5baf_write_arr_seq(state, addr, count, nseq);
  458. nseq += count;
  459. }
  460. }
  461. static void s5k5baf_synchronize(struct s5k5baf *state, int timeout, u16 addr)
  462. {
  463. unsigned long end = jiffies + msecs_to_jiffies(timeout);
  464. u16 reg;
  465. s5k5baf_write(state, addr, 1);
  466. do {
  467. reg = s5k5baf_read(state, addr);
  468. if (state->error || !reg)
  469. return;
  470. usleep_range(5000, 10000);
  471. } while (time_is_after_jiffies(end));
  472. v4l2_err(&state->sd, "timeout on register synchronize (%#x)\n", addr);
  473. state->error = -ETIMEDOUT;
  474. }
  475. static u16 *s5k5baf_fw_get_seq(struct s5k5baf *state, u16 seq_id)
  476. {
  477. struct s5k5baf_fw *fw = state->fw;
  478. u16 *data;
  479. int i;
  480. if (fw == NULL)
  481. return NULL;
  482. data = fw->data + 2 * fw->count;
  483. for (i = 0; i < fw->count; ++i) {
  484. if (fw->seq[i].id == seq_id)
  485. return data + fw->seq[i].offset;
  486. }
  487. return NULL;
  488. }
  489. static void s5k5baf_hw_patch(struct s5k5baf *state)
  490. {
  491. u16 *seq = s5k5baf_fw_get_seq(state, S5K5BAF_FW_ID_PATCH);
  492. if (seq)
  493. s5k5baf_write_nseq(state, seq);
  494. }
  495. static void s5k5baf_hw_set_clocks(struct s5k5baf *state)
  496. {
  497. unsigned long mclk = state->mclk_frequency / 1000;
  498. u16 status;
  499. static const u16 nseq_clk_cfg[] = {
  500. NSEQ(REG_I_USE_NPVI_CLOCKS,
  501. NPVI_CLOCKS, NMIPI_CLOCKS, 0,
  502. SCLK_PVI_FREQ / 4, PCLK_MIN_FREQ / 4, PCLK_MAX_FREQ / 4,
  503. SCLK_MIPI_FREQ / 4, PCLK_MIN_FREQ / 4, PCLK_MAX_FREQ / 4),
  504. NSEQ(REG_I_USE_REGS_API, 1),
  505. 0
  506. };
  507. s5k5baf_write_seq(state, REG_I_INCLK_FREQ_L, mclk & 0xffff, mclk >> 16);
  508. s5k5baf_write_nseq(state, nseq_clk_cfg);
  509. s5k5baf_synchronize(state, 250, REG_I_INIT_PARAMS_UPDATED);
  510. status = s5k5baf_read(state, REG_I_ERROR_INFO);
  511. if (!state->error && status) {
  512. v4l2_err(&state->sd, "error configuring PLL (%d)\n", status);
  513. state->error = -EINVAL;
  514. }
  515. }
  516. /* set custom color correction matrices for various illuminations */
  517. static void s5k5baf_hw_set_ccm(struct s5k5baf *state)
  518. {
  519. u16 *seq = s5k5baf_fw_get_seq(state, S5K5BAF_FW_ID_CCM);
  520. if (seq)
  521. s5k5baf_write_nseq(state, seq);
  522. }
  523. /* CIS sensor tuning, based on undocumented android driver code */
  524. static void s5k5baf_hw_set_cis(struct s5k5baf *state)
  525. {
  526. u16 *seq = s5k5baf_fw_get_seq(state, S5K5BAF_FW_ID_CIS);
  527. if (!seq)
  528. return;
  529. s5k5baf_i2c_write(state, REG_CMDWR_PAGE, PAGE_IF_HW);
  530. s5k5baf_write_nseq(state, seq);
  531. s5k5baf_i2c_write(state, REG_CMDWR_PAGE, PAGE_IF_SW);
  532. }
  533. static void s5k5baf_hw_sync_cfg(struct s5k5baf *state)
  534. {
  535. s5k5baf_write(state, REG_G_PREV_CFG_CHG, 1);
  536. if (state->apply_crop) {
  537. s5k5baf_write(state, REG_G_INPUTS_CHANGE_REQ, 1);
  538. s5k5baf_write(state, REG_G_PREV_CFG_BYPASS_CHANGED, 1);
  539. }
  540. s5k5baf_synchronize(state, 500, REG_G_NEW_CFG_SYNC);
  541. }
  542. /* Set horizontal and vertical image flipping */
  543. static void s5k5baf_hw_set_mirror(struct s5k5baf *state)
  544. {
  545. u16 flip = state->ctrls.vflip->val | (state->ctrls.vflip->val << 1);
  546. s5k5baf_write(state, REG_P_PREV_MIRROR(0), flip);
  547. if (state->streaming)
  548. s5k5baf_hw_sync_cfg(state);
  549. }
  550. static void s5k5baf_hw_set_alg(struct s5k5baf *state, u16 alg, bool enable)
  551. {
  552. u16 cur_alg, new_alg;
  553. if (!state->valid_auto_alg)
  554. cur_alg = s5k5baf_read(state, REG_DBG_AUTOALG_EN);
  555. else
  556. cur_alg = state->auto_alg;
  557. new_alg = enable ? (cur_alg | alg) : (cur_alg & ~alg);
  558. if (new_alg != cur_alg)
  559. s5k5baf_write(state, REG_DBG_AUTOALG_EN, new_alg);
  560. if (state->error)
  561. return;
  562. state->valid_auto_alg = 1;
  563. state->auto_alg = new_alg;
  564. }
  565. /* Configure auto/manual white balance and R/G/B gains */
  566. static void s5k5baf_hw_set_awb(struct s5k5baf *state, int awb)
  567. {
  568. struct s5k5baf_ctrls *ctrls = &state->ctrls;
  569. if (!awb)
  570. s5k5baf_write_seq(state, REG_SF_RGAIN,
  571. ctrls->gain_red->val, 1,
  572. S5K5BAF_GAIN_GREEN_DEF, 1,
  573. ctrls->gain_blue->val, 1,
  574. 1);
  575. s5k5baf_hw_set_alg(state, AALG_WB_EN, awb);
  576. }
  577. /* Program FW with exposure time, 'exposure' in us units */
  578. static void s5k5baf_hw_set_user_exposure(struct s5k5baf *state, int exposure)
  579. {
  580. unsigned int time = exposure / 10;
  581. s5k5baf_write_seq(state, REG_SF_USR_EXPOSURE_L,
  582. time & 0xffff, time >> 16, 1);
  583. }
  584. static void s5k5baf_hw_set_user_gain(struct s5k5baf *state, int gain)
  585. {
  586. s5k5baf_write_seq(state, REG_SF_USR_TOT_GAIN, gain, 1);
  587. }
  588. /* Set auto/manual exposure and total gain */
  589. static void s5k5baf_hw_set_auto_exposure(struct s5k5baf *state, int value)
  590. {
  591. if (value == V4L2_EXPOSURE_AUTO) {
  592. s5k5baf_hw_set_alg(state, AALG_AE_EN | AALG_DIVLEI_EN, true);
  593. } else {
  594. unsigned int exp_time = state->ctrls.exposure->val;
  595. s5k5baf_hw_set_user_exposure(state, exp_time);
  596. s5k5baf_hw_set_user_gain(state, state->ctrls.gain->val);
  597. s5k5baf_hw_set_alg(state, AALG_AE_EN | AALG_DIVLEI_EN, false);
  598. }
  599. }
  600. static void s5k5baf_hw_set_anti_flicker(struct s5k5baf *state, int v)
  601. {
  602. if (v == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) {
  603. s5k5baf_hw_set_alg(state, AALG_FLICKER_EN, true);
  604. } else {
  605. /* The V4L2_CID_LINE_FREQUENCY control values match
  606. * the register values */
  607. s5k5baf_write_seq(state, REG_SF_FLICKER_QUANT, v, 1);
  608. s5k5baf_hw_set_alg(state, AALG_FLICKER_EN, false);
  609. }
  610. }
  611. static void s5k5baf_hw_set_colorfx(struct s5k5baf *state, int val)
  612. {
  613. static const u16 colorfx[] = {
  614. [V4L2_COLORFX_NONE] = 0,
  615. [V4L2_COLORFX_BW] = 1,
  616. [V4L2_COLORFX_NEGATIVE] = 2,
  617. [V4L2_COLORFX_SEPIA] = 3,
  618. [V4L2_COLORFX_SKY_BLUE] = 4,
  619. [V4L2_COLORFX_SKETCH] = 5,
  620. };
  621. s5k5baf_write(state, REG_G_SPEC_EFFECTS, colorfx[val]);
  622. }
  623. static int s5k5baf_find_pixfmt(struct v4l2_mbus_framefmt *mf)
  624. {
  625. int i, c = -1;
  626. for (i = 0; i < ARRAY_SIZE(s5k5baf_formats); i++) {
  627. if (mf->colorspace != s5k5baf_formats[i].colorspace)
  628. continue;
  629. if (mf->code == s5k5baf_formats[i].code)
  630. return i;
  631. if (c < 0)
  632. c = i;
  633. }
  634. return (c < 0) ? 0 : c;
  635. }
  636. static int s5k5baf_clear_error(struct s5k5baf *state)
  637. {
  638. int ret = state->error;
  639. state->error = 0;
  640. return ret;
  641. }
  642. static int s5k5baf_hw_set_video_bus(struct s5k5baf *state)
  643. {
  644. u16 en_pkts;
  645. if (state->bus_type == V4L2_MBUS_CSI2)
  646. en_pkts = EN_PACKETS_CSI2;
  647. else
  648. en_pkts = 0;
  649. s5k5baf_write_seq(state, REG_OIF_EN_MIPI_LANES,
  650. state->nlanes, en_pkts, 1);
  651. return s5k5baf_clear_error(state);
  652. }
  653. static u16 s5k5baf_get_cfg_error(struct s5k5baf *state)
  654. {
  655. u16 err = s5k5baf_read(state, REG_G_PREV_CFG_ERROR);
  656. if (err)
  657. s5k5baf_write(state, REG_G_PREV_CFG_ERROR, 0);
  658. return err;
  659. }
  660. static void s5k5baf_hw_set_fiv(struct s5k5baf *state, u16 fiv)
  661. {
  662. s5k5baf_write(state, REG_P_MAX_FR_TIME(0), fiv);
  663. s5k5baf_hw_sync_cfg(state);
  664. }
  665. static void s5k5baf_hw_find_min_fiv(struct s5k5baf *state)
  666. {
  667. u16 err, fiv;
  668. int n;
  669. fiv = s5k5baf_read(state, REG_G_ACTUAL_P_FR_TIME);
  670. if (state->error)
  671. return;
  672. for (n = 5; n > 0; --n) {
  673. s5k5baf_hw_set_fiv(state, fiv);
  674. err = s5k5baf_get_cfg_error(state);
  675. if (state->error)
  676. return;
  677. switch (err) {
  678. case CFG_ERROR_RANGE:
  679. ++fiv;
  680. break;
  681. case 0:
  682. state->fiv = fiv;
  683. v4l2_info(&state->sd,
  684. "found valid frame interval: %d00us\n", fiv);
  685. return;
  686. default:
  687. v4l2_err(&state->sd,
  688. "error setting frame interval: %d\n", err);
  689. state->error = -EINVAL;
  690. }
  691. };
  692. v4l2_err(&state->sd, "cannot find correct frame interval\n");
  693. state->error = -ERANGE;
  694. }
  695. static void s5k5baf_hw_validate_cfg(struct s5k5baf *state)
  696. {
  697. u16 err;
  698. err = s5k5baf_get_cfg_error(state);
  699. if (state->error)
  700. return;
  701. switch (err) {
  702. case 0:
  703. state->apply_cfg = 1;
  704. return;
  705. case CFG_ERROR_RANGE:
  706. s5k5baf_hw_find_min_fiv(state);
  707. if (!state->error)
  708. state->apply_cfg = 1;
  709. return;
  710. default:
  711. v4l2_err(&state->sd,
  712. "error setting format: %d\n", err);
  713. state->error = -EINVAL;
  714. }
  715. }
  716. static void s5k5baf_rescale(struct v4l2_rect *r, const struct v4l2_rect *v,
  717. const struct v4l2_rect *n,
  718. const struct v4l2_rect *d)
  719. {
  720. r->left = v->left * n->width / d->width;
  721. r->top = v->top * n->height / d->height;
  722. r->width = v->width * n->width / d->width;
  723. r->height = v->height * n->height / d->height;
  724. }
  725. static int s5k5baf_hw_set_crop_rects(struct s5k5baf *state)
  726. {
  727. struct v4l2_rect *p, r;
  728. u16 err;
  729. int ret;
  730. p = &state->crop_sink;
  731. s5k5baf_write_seq(state, REG_G_PREVREQ_IN_WIDTH, p->width, p->height,
  732. p->left, p->top);
  733. s5k5baf_rescale(&r, &state->crop_source, &state->crop_sink,
  734. &state->compose);
  735. s5k5baf_write_seq(state, REG_G_PREVZOOM_IN_WIDTH, r.width, r.height,
  736. r.left, r.top);
  737. s5k5baf_synchronize(state, 500, REG_G_INPUTS_CHANGE_REQ);
  738. s5k5baf_synchronize(state, 500, REG_G_PREV_CFG_BYPASS_CHANGED);
  739. err = s5k5baf_get_cfg_error(state);
  740. ret = s5k5baf_clear_error(state);
  741. if (ret < 0)
  742. return ret;
  743. switch (err) {
  744. case 0:
  745. break;
  746. case CFG_ERROR_RANGE:
  747. /* retry crop with frame interval set to max */
  748. s5k5baf_hw_set_fiv(state, S5K5BAF_MAX_FR_TIME);
  749. err = s5k5baf_get_cfg_error(state);
  750. ret = s5k5baf_clear_error(state);
  751. if (ret < 0)
  752. return ret;
  753. if (err) {
  754. v4l2_err(&state->sd,
  755. "crop error on max frame interval: %d\n", err);
  756. state->error = -EINVAL;
  757. }
  758. s5k5baf_hw_set_fiv(state, state->req_fiv);
  759. s5k5baf_hw_validate_cfg(state);
  760. break;
  761. default:
  762. v4l2_err(&state->sd, "crop error: %d\n", err);
  763. return -EINVAL;
  764. }
  765. if (!state->apply_cfg)
  766. return 0;
  767. p = &state->crop_source;
  768. s5k5baf_write_seq(state, REG_P_OUT_WIDTH(0), p->width, p->height);
  769. s5k5baf_hw_set_fiv(state, state->req_fiv);
  770. s5k5baf_hw_validate_cfg(state);
  771. return s5k5baf_clear_error(state);
  772. }
  773. static void s5k5baf_hw_set_config(struct s5k5baf *state)
  774. {
  775. u16 reg_fmt = s5k5baf_formats[state->pixfmt].reg_p_fmt;
  776. struct v4l2_rect *r = &state->crop_source;
  777. s5k5baf_write_seq(state, REG_P_OUT_WIDTH(0),
  778. r->width, r->height, reg_fmt,
  779. PCLK_MAX_FREQ >> 2, PCLK_MIN_FREQ >> 2,
  780. PVI_MASK_MIPI, CLK_MIPI_INDEX,
  781. FR_RATE_FIXED, FR_RATE_Q_DYNAMIC,
  782. state->req_fiv, S5K5BAF_MIN_FR_TIME);
  783. s5k5baf_hw_sync_cfg(state);
  784. s5k5baf_hw_validate_cfg(state);
  785. }
  786. static void s5k5baf_hw_set_test_pattern(struct s5k5baf *state, int id)
  787. {
  788. s5k5baf_i2c_write(state, REG_PATTERN_WIDTH, 800);
  789. s5k5baf_i2c_write(state, REG_PATTERN_HEIGHT, 511);
  790. s5k5baf_i2c_write(state, REG_PATTERN_PARAM, 0);
  791. s5k5baf_i2c_write(state, REG_PATTERN_SET, id);
  792. }
  793. static void s5k5baf_gpio_assert(struct s5k5baf *state, int id)
  794. {
  795. struct s5k5baf_gpio *gpio = &state->gpios[id];
  796. gpio_set_value(gpio->gpio, gpio->level);
  797. }
  798. static void s5k5baf_gpio_deassert(struct s5k5baf *state, int id)
  799. {
  800. struct s5k5baf_gpio *gpio = &state->gpios[id];
  801. gpio_set_value(gpio->gpio, !gpio->level);
  802. }
  803. static int s5k5baf_power_on(struct s5k5baf *state)
  804. {
  805. int ret;
  806. ret = regulator_bulk_enable(S5K5BAF_NUM_SUPPLIES, state->supplies);
  807. if (ret < 0)
  808. goto err;
  809. ret = clk_set_rate(state->clock, state->mclk_frequency);
  810. if (ret < 0)
  811. goto err_reg_dis;
  812. ret = clk_prepare_enable(state->clock);
  813. if (ret < 0)
  814. goto err_reg_dis;
  815. v4l2_dbg(1, debug, &state->sd, "clock frequency: %ld\n",
  816. clk_get_rate(state->clock));
  817. s5k5baf_gpio_deassert(state, STBY);
  818. usleep_range(50, 100);
  819. s5k5baf_gpio_deassert(state, RST);
  820. return 0;
  821. err_reg_dis:
  822. regulator_bulk_disable(S5K5BAF_NUM_SUPPLIES, state->supplies);
  823. err:
  824. v4l2_err(&state->sd, "%s() failed (%d)\n", __func__, ret);
  825. return ret;
  826. }
  827. static int s5k5baf_power_off(struct s5k5baf *state)
  828. {
  829. int ret;
  830. state->streaming = 0;
  831. state->apply_cfg = 0;
  832. state->apply_crop = 0;
  833. s5k5baf_gpio_assert(state, RST);
  834. s5k5baf_gpio_assert(state, STBY);
  835. if (!IS_ERR(state->clock))
  836. clk_disable_unprepare(state->clock);
  837. ret = regulator_bulk_disable(S5K5BAF_NUM_SUPPLIES,
  838. state->supplies);
  839. if (ret < 0)
  840. v4l2_err(&state->sd, "failed to disable regulators\n");
  841. return 0;
  842. }
  843. static void s5k5baf_hw_init(struct s5k5baf *state)
  844. {
  845. s5k5baf_i2c_write(state, AHB_MSB_ADDR_PTR, PAGE_IF_HW);
  846. s5k5baf_i2c_write(state, REG_CLEAR_HOST_INT, 0);
  847. s5k5baf_i2c_write(state, REG_SW_LOAD_COMPLETE, 1);
  848. s5k5baf_i2c_write(state, REG_CMDRD_PAGE, PAGE_IF_SW);
  849. s5k5baf_i2c_write(state, REG_CMDWR_PAGE, PAGE_IF_SW);
  850. }
  851. /*
  852. * V4L2 subdev core and video operations
  853. */
  854. static void s5k5baf_initialize_data(struct s5k5baf *state)
  855. {
  856. state->pixfmt = 0;
  857. state->req_fiv = 10000 / 15;
  858. state->fiv = state->req_fiv;
  859. state->valid_auto_alg = 0;
  860. }
  861. static int s5k5baf_load_setfile(struct s5k5baf *state)
  862. {
  863. struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
  864. const struct firmware *fw;
  865. int ret;
  866. ret = request_firmware(&fw, S5K5BAF_FW_FILENAME, &c->dev);
  867. if (ret < 0) {
  868. dev_warn(&c->dev, "firmware file (%s) not loaded\n",
  869. S5K5BAF_FW_FILENAME);
  870. return ret;
  871. }
  872. ret = s5k5baf_fw_parse(&c->dev, &state->fw, fw->size / 2,
  873. (u16 *)fw->data);
  874. release_firmware(fw);
  875. return ret;
  876. }
  877. static int s5k5baf_set_power(struct v4l2_subdev *sd, int on)
  878. {
  879. struct s5k5baf *state = to_s5k5baf(sd);
  880. int ret = 0;
  881. mutex_lock(&state->lock);
  882. if (!on != state->power)
  883. goto out;
  884. if (on) {
  885. if (state->fw == NULL)
  886. s5k5baf_load_setfile(state);
  887. s5k5baf_initialize_data(state);
  888. ret = s5k5baf_power_on(state);
  889. if (ret < 0)
  890. goto out;
  891. s5k5baf_hw_init(state);
  892. s5k5baf_hw_patch(state);
  893. s5k5baf_i2c_write(state, REG_SET_HOST_INT, 1);
  894. s5k5baf_hw_set_clocks(state);
  895. ret = s5k5baf_hw_set_video_bus(state);
  896. if (ret < 0)
  897. goto out;
  898. s5k5baf_hw_set_cis(state);
  899. s5k5baf_hw_set_ccm(state);
  900. ret = s5k5baf_clear_error(state);
  901. if (!ret)
  902. state->power++;
  903. } else {
  904. s5k5baf_power_off(state);
  905. state->power--;
  906. }
  907. out:
  908. mutex_unlock(&state->lock);
  909. if (!ret && on)
  910. ret = v4l2_ctrl_handler_setup(&state->ctrls.handler);
  911. return ret;
  912. }
  913. static void s5k5baf_hw_set_stream(struct s5k5baf *state, int enable)
  914. {
  915. s5k5baf_write_seq(state, REG_G_ENABLE_PREV, enable, 1);
  916. }
  917. static int s5k5baf_s_stream(struct v4l2_subdev *sd, int on)
  918. {
  919. struct s5k5baf *state = to_s5k5baf(sd);
  920. int ret;
  921. mutex_lock(&state->lock);
  922. if (state->streaming == !!on) {
  923. ret = 0;
  924. goto out;
  925. }
  926. if (on) {
  927. s5k5baf_hw_set_config(state);
  928. ret = s5k5baf_hw_set_crop_rects(state);
  929. if (ret < 0)
  930. goto out;
  931. s5k5baf_hw_set_stream(state, 1);
  932. s5k5baf_i2c_write(state, 0xb0cc, 0x000b);
  933. } else {
  934. s5k5baf_hw_set_stream(state, 0);
  935. }
  936. ret = s5k5baf_clear_error(state);
  937. if (!ret)
  938. state->streaming = !state->streaming;
  939. out:
  940. mutex_unlock(&state->lock);
  941. return ret;
  942. }
  943. static int s5k5baf_g_frame_interval(struct v4l2_subdev *sd,
  944. struct v4l2_subdev_frame_interval *fi)
  945. {
  946. struct s5k5baf *state = to_s5k5baf(sd);
  947. mutex_lock(&state->lock);
  948. fi->interval.numerator = state->fiv;
  949. fi->interval.denominator = 10000;
  950. mutex_unlock(&state->lock);
  951. return 0;
  952. }
  953. static void s5k5baf_set_frame_interval(struct s5k5baf *state,
  954. struct v4l2_subdev_frame_interval *fi)
  955. {
  956. struct v4l2_fract *i = &fi->interval;
  957. if (fi->interval.denominator == 0)
  958. state->req_fiv = S5K5BAF_MAX_FR_TIME;
  959. else
  960. state->req_fiv = clamp_t(u32,
  961. i->numerator * 10000 / i->denominator,
  962. S5K5BAF_MIN_FR_TIME,
  963. S5K5BAF_MAX_FR_TIME);
  964. state->fiv = state->req_fiv;
  965. if (state->apply_cfg) {
  966. s5k5baf_hw_set_fiv(state, state->req_fiv);
  967. s5k5baf_hw_validate_cfg(state);
  968. }
  969. *i = (struct v4l2_fract){ state->fiv, 10000 };
  970. if (state->fiv == state->req_fiv)
  971. v4l2_info(&state->sd, "frame interval changed to %d00us\n",
  972. state->fiv);
  973. }
  974. static int s5k5baf_s_frame_interval(struct v4l2_subdev *sd,
  975. struct v4l2_subdev_frame_interval *fi)
  976. {
  977. struct s5k5baf *state = to_s5k5baf(sd);
  978. mutex_lock(&state->lock);
  979. s5k5baf_set_frame_interval(state, fi);
  980. mutex_unlock(&state->lock);
  981. return 0;
  982. }
  983. /*
  984. * V4L2 subdev pad level and video operations
  985. */
  986. static int s5k5baf_enum_frame_interval(struct v4l2_subdev *sd,
  987. struct v4l2_subdev_fh *fh,
  988. struct v4l2_subdev_frame_interval_enum *fie)
  989. {
  990. if (fie->index > S5K5BAF_MAX_FR_TIME - S5K5BAF_MIN_FR_TIME ||
  991. fie->pad != PAD_CIS)
  992. return -EINVAL;
  993. v4l_bound_align_image(&fie->width, S5K5BAF_WIN_WIDTH_MIN,
  994. S5K5BAF_CIS_WIDTH, 1,
  995. &fie->height, S5K5BAF_WIN_HEIGHT_MIN,
  996. S5K5BAF_CIS_HEIGHT, 1, 0);
  997. fie->interval.numerator = S5K5BAF_MIN_FR_TIME + fie->index;
  998. fie->interval.denominator = 10000;
  999. return 0;
  1000. }
  1001. static int s5k5baf_enum_mbus_code(struct v4l2_subdev *sd,
  1002. struct v4l2_subdev_fh *fh,
  1003. struct v4l2_subdev_mbus_code_enum *code)
  1004. {
  1005. if (code->pad == PAD_CIS) {
  1006. if (code->index > 0)
  1007. return -EINVAL;
  1008. code->code = V4L2_MBUS_FMT_FIXED;
  1009. return 0;
  1010. }
  1011. if (code->index >= ARRAY_SIZE(s5k5baf_formats))
  1012. return -EINVAL;
  1013. code->code = s5k5baf_formats[code->index].code;
  1014. return 0;
  1015. }
  1016. static int s5k5baf_enum_frame_size(struct v4l2_subdev *sd,
  1017. struct v4l2_subdev_fh *fh,
  1018. struct v4l2_subdev_frame_size_enum *fse)
  1019. {
  1020. int i;
  1021. if (fse->index > 0)
  1022. return -EINVAL;
  1023. if (fse->pad == PAD_CIS) {
  1024. fse->code = V4L2_MBUS_FMT_FIXED;
  1025. fse->min_width = S5K5BAF_CIS_WIDTH;
  1026. fse->max_width = S5K5BAF_CIS_WIDTH;
  1027. fse->min_height = S5K5BAF_CIS_HEIGHT;
  1028. fse->max_height = S5K5BAF_CIS_HEIGHT;
  1029. return 0;
  1030. }
  1031. i = ARRAY_SIZE(s5k5baf_formats);
  1032. while (--i)
  1033. if (fse->code == s5k5baf_formats[i].code)
  1034. break;
  1035. fse->code = s5k5baf_formats[i].code;
  1036. fse->min_width = S5K5BAF_WIN_WIDTH_MIN;
  1037. fse->max_width = S5K5BAF_CIS_WIDTH;
  1038. fse->max_height = S5K5BAF_WIN_HEIGHT_MIN;
  1039. fse->min_height = S5K5BAF_CIS_HEIGHT;
  1040. return 0;
  1041. }
  1042. static void s5k5baf_try_cis_format(struct v4l2_mbus_framefmt *mf)
  1043. {
  1044. mf->width = S5K5BAF_CIS_WIDTH;
  1045. mf->height = S5K5BAF_CIS_HEIGHT;
  1046. mf->code = V4L2_MBUS_FMT_FIXED;
  1047. mf->colorspace = V4L2_COLORSPACE_JPEG;
  1048. mf->field = V4L2_FIELD_NONE;
  1049. }
  1050. static int s5k5baf_try_isp_format(struct v4l2_mbus_framefmt *mf)
  1051. {
  1052. int pixfmt;
  1053. v4l_bound_align_image(&mf->width, S5K5BAF_WIN_WIDTH_MIN,
  1054. S5K5BAF_CIS_WIDTH, 1,
  1055. &mf->height, S5K5BAF_WIN_HEIGHT_MIN,
  1056. S5K5BAF_CIS_HEIGHT, 1, 0);
  1057. pixfmt = s5k5baf_find_pixfmt(mf);
  1058. mf->colorspace = s5k5baf_formats[pixfmt].colorspace;
  1059. mf->code = s5k5baf_formats[pixfmt].code;
  1060. mf->field = V4L2_FIELD_NONE;
  1061. return pixfmt;
  1062. }
  1063. static int s5k5baf_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  1064. struct v4l2_subdev_format *fmt)
  1065. {
  1066. struct s5k5baf *state = to_s5k5baf(sd);
  1067. const struct s5k5baf_pixfmt *pixfmt;
  1068. struct v4l2_mbus_framefmt *mf;
  1069. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1070. mf = v4l2_subdev_get_try_format(fh, fmt->pad);
  1071. fmt->format = *mf;
  1072. return 0;
  1073. }
  1074. mf = &fmt->format;
  1075. if (fmt->pad == PAD_CIS) {
  1076. s5k5baf_try_cis_format(mf);
  1077. return 0;
  1078. }
  1079. mf->field = V4L2_FIELD_NONE;
  1080. mutex_lock(&state->lock);
  1081. pixfmt = &s5k5baf_formats[state->pixfmt];
  1082. mf->width = state->crop_source.width;
  1083. mf->height = state->crop_source.height;
  1084. mf->code = pixfmt->code;
  1085. mf->colorspace = pixfmt->colorspace;
  1086. mutex_unlock(&state->lock);
  1087. return 0;
  1088. }
  1089. static int s5k5baf_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  1090. struct v4l2_subdev_format *fmt)
  1091. {
  1092. struct v4l2_mbus_framefmt *mf = &fmt->format;
  1093. struct s5k5baf *state = to_s5k5baf(sd);
  1094. const struct s5k5baf_pixfmt *pixfmt;
  1095. int ret = 0;
  1096. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1097. *v4l2_subdev_get_try_format(fh, fmt->pad) = *mf;
  1098. return 0;
  1099. }
  1100. if (fmt->pad == PAD_CIS) {
  1101. s5k5baf_try_cis_format(mf);
  1102. return 0;
  1103. }
  1104. mutex_lock(&state->lock);
  1105. if (state->streaming) {
  1106. mutex_unlock(&state->lock);
  1107. return -EBUSY;
  1108. }
  1109. state->pixfmt = s5k5baf_try_isp_format(mf);
  1110. pixfmt = &s5k5baf_formats[state->pixfmt];
  1111. mf->code = pixfmt->code;
  1112. mf->colorspace = pixfmt->colorspace;
  1113. mf->width = state->crop_source.width;
  1114. mf->height = state->crop_source.height;
  1115. mutex_unlock(&state->lock);
  1116. return ret;
  1117. }
  1118. enum selection_rect { R_CIS, R_CROP_SINK, R_COMPOSE, R_CROP_SOURCE, R_INVALID };
  1119. static enum selection_rect s5k5baf_get_sel_rect(u32 pad, u32 target)
  1120. {
  1121. switch (target) {
  1122. case V4L2_SEL_TGT_CROP_BOUNDS:
  1123. return pad ? R_COMPOSE : R_CIS;
  1124. case V4L2_SEL_TGT_CROP:
  1125. return pad ? R_CROP_SOURCE : R_CROP_SINK;
  1126. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  1127. return pad ? R_INVALID : R_CROP_SINK;
  1128. case V4L2_SEL_TGT_COMPOSE:
  1129. return pad ? R_INVALID : R_COMPOSE;
  1130. default:
  1131. return R_INVALID;
  1132. }
  1133. }
  1134. static int s5k5baf_is_bound_target(u32 target)
  1135. {
  1136. return target == V4L2_SEL_TGT_CROP_BOUNDS ||
  1137. target == V4L2_SEL_TGT_COMPOSE_BOUNDS;
  1138. }
  1139. static int s5k5baf_get_selection(struct v4l2_subdev *sd,
  1140. struct v4l2_subdev_fh *fh,
  1141. struct v4l2_subdev_selection *sel)
  1142. {
  1143. static enum selection_rect rtype;
  1144. struct s5k5baf *state = to_s5k5baf(sd);
  1145. rtype = s5k5baf_get_sel_rect(sel->pad, sel->target);
  1146. switch (rtype) {
  1147. case R_INVALID:
  1148. return -EINVAL;
  1149. case R_CIS:
  1150. sel->r = s5k5baf_cis_rect;
  1151. return 0;
  1152. default:
  1153. break;
  1154. }
  1155. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  1156. if (rtype == R_COMPOSE)
  1157. sel->r = *v4l2_subdev_get_try_compose(fh, sel->pad);
  1158. else
  1159. sel->r = *v4l2_subdev_get_try_crop(fh, sel->pad);
  1160. return 0;
  1161. }
  1162. mutex_lock(&state->lock);
  1163. switch (rtype) {
  1164. case R_CROP_SINK:
  1165. sel->r = state->crop_sink;
  1166. break;
  1167. case R_COMPOSE:
  1168. sel->r = state->compose;
  1169. break;
  1170. case R_CROP_SOURCE:
  1171. sel->r = state->crop_source;
  1172. break;
  1173. default:
  1174. break;
  1175. }
  1176. if (s5k5baf_is_bound_target(sel->target)) {
  1177. sel->r.left = 0;
  1178. sel->r.top = 0;
  1179. }
  1180. mutex_unlock(&state->lock);
  1181. return 0;
  1182. }
  1183. /* bounds range [start, start+len) to [0, max) and aligns to 2 */
  1184. static void s5k5baf_bound_range(u32 *start, u32 *len, u32 max)
  1185. {
  1186. if (*len > max)
  1187. *len = max;
  1188. if (*start + *len > max)
  1189. *start = max - *len;
  1190. *start &= ~1;
  1191. *len &= ~1;
  1192. if (*len < S5K5BAF_WIN_WIDTH_MIN)
  1193. *len = S5K5BAF_WIN_WIDTH_MIN;
  1194. }
  1195. static void s5k5baf_bound_rect(struct v4l2_rect *r, u32 width, u32 height)
  1196. {
  1197. s5k5baf_bound_range(&r->left, &r->width, width);
  1198. s5k5baf_bound_range(&r->top, &r->height, height);
  1199. }
  1200. static void s5k5baf_set_rect_and_adjust(struct v4l2_rect **rects,
  1201. enum selection_rect first,
  1202. struct v4l2_rect *v)
  1203. {
  1204. struct v4l2_rect *r, *br;
  1205. enum selection_rect i = first;
  1206. *rects[first] = *v;
  1207. do {
  1208. r = rects[i];
  1209. br = rects[i - 1];
  1210. s5k5baf_bound_rect(r, br->width, br->height);
  1211. } while (++i != R_INVALID);
  1212. *v = *rects[first];
  1213. }
  1214. static bool s5k5baf_cmp_rect(const struct v4l2_rect *r1,
  1215. const struct v4l2_rect *r2)
  1216. {
  1217. return !memcmp(r1, r2, sizeof(*r1));
  1218. }
  1219. static int s5k5baf_set_selection(struct v4l2_subdev *sd,
  1220. struct v4l2_subdev_fh *fh,
  1221. struct v4l2_subdev_selection *sel)
  1222. {
  1223. static enum selection_rect rtype;
  1224. struct s5k5baf *state = to_s5k5baf(sd);
  1225. struct v4l2_rect **rects;
  1226. int ret = 0;
  1227. rtype = s5k5baf_get_sel_rect(sel->pad, sel->target);
  1228. if (rtype == R_INVALID || s5k5baf_is_bound_target(sel->target))
  1229. return -EINVAL;
  1230. /* allow only scaling on compose */
  1231. if (rtype == R_COMPOSE) {
  1232. sel->r.left = 0;
  1233. sel->r.top = 0;
  1234. }
  1235. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  1236. rects = (struct v4l2_rect * []) {
  1237. &s5k5baf_cis_rect,
  1238. v4l2_subdev_get_try_crop(fh, PAD_CIS),
  1239. v4l2_subdev_get_try_compose(fh, PAD_CIS),
  1240. v4l2_subdev_get_try_crop(fh, PAD_OUT)
  1241. };
  1242. s5k5baf_set_rect_and_adjust(rects, rtype, &sel->r);
  1243. return 0;
  1244. }
  1245. rects = (struct v4l2_rect * []) {
  1246. &s5k5baf_cis_rect,
  1247. &state->crop_sink,
  1248. &state->compose,
  1249. &state->crop_source
  1250. };
  1251. mutex_lock(&state->lock);
  1252. if (state->streaming) {
  1253. /* adjust sel->r to avoid output resolution change */
  1254. if (rtype < R_CROP_SOURCE) {
  1255. if (sel->r.width < state->crop_source.width)
  1256. sel->r.width = state->crop_source.width;
  1257. if (sel->r.height < state->crop_source.height)
  1258. sel->r.height = state->crop_source.height;
  1259. } else {
  1260. sel->r.width = state->crop_source.width;
  1261. sel->r.height = state->crop_source.height;
  1262. }
  1263. }
  1264. s5k5baf_set_rect_and_adjust(rects, rtype, &sel->r);
  1265. if (!s5k5baf_cmp_rect(&state->crop_sink, &s5k5baf_cis_rect) ||
  1266. !s5k5baf_cmp_rect(&state->compose, &s5k5baf_cis_rect))
  1267. state->apply_crop = 1;
  1268. if (state->streaming)
  1269. ret = s5k5baf_hw_set_crop_rects(state);
  1270. mutex_unlock(&state->lock);
  1271. return ret;
  1272. }
  1273. static const struct v4l2_subdev_pad_ops s5k5baf_cis_pad_ops = {
  1274. .enum_mbus_code = s5k5baf_enum_mbus_code,
  1275. .enum_frame_size = s5k5baf_enum_frame_size,
  1276. .get_fmt = s5k5baf_get_fmt,
  1277. .set_fmt = s5k5baf_set_fmt,
  1278. };
  1279. static const struct v4l2_subdev_pad_ops s5k5baf_pad_ops = {
  1280. .enum_mbus_code = s5k5baf_enum_mbus_code,
  1281. .enum_frame_size = s5k5baf_enum_frame_size,
  1282. .enum_frame_interval = s5k5baf_enum_frame_interval,
  1283. .get_fmt = s5k5baf_get_fmt,
  1284. .set_fmt = s5k5baf_set_fmt,
  1285. .get_selection = s5k5baf_get_selection,
  1286. .set_selection = s5k5baf_set_selection,
  1287. };
  1288. static const struct v4l2_subdev_video_ops s5k5baf_video_ops = {
  1289. .g_frame_interval = s5k5baf_g_frame_interval,
  1290. .s_frame_interval = s5k5baf_s_frame_interval,
  1291. .s_stream = s5k5baf_s_stream,
  1292. };
  1293. /*
  1294. * V4L2 subdev controls
  1295. */
  1296. static int s5k5baf_s_ctrl(struct v4l2_ctrl *ctrl)
  1297. {
  1298. struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
  1299. struct s5k5baf *state = to_s5k5baf(sd);
  1300. int ret;
  1301. v4l2_dbg(1, debug, sd, "ctrl: %s, value: %d\n", ctrl->name, ctrl->val);
  1302. mutex_lock(&state->lock);
  1303. if (state->power == 0)
  1304. goto unlock;
  1305. switch (ctrl->id) {
  1306. case V4L2_CID_AUTO_WHITE_BALANCE:
  1307. s5k5baf_hw_set_awb(state, ctrl->val);
  1308. break;
  1309. case V4L2_CID_BRIGHTNESS:
  1310. s5k5baf_write(state, REG_USER_BRIGHTNESS, ctrl->val);
  1311. break;
  1312. case V4L2_CID_COLORFX:
  1313. s5k5baf_hw_set_colorfx(state, ctrl->val);
  1314. break;
  1315. case V4L2_CID_CONTRAST:
  1316. s5k5baf_write(state, REG_USER_CONTRAST, ctrl->val);
  1317. break;
  1318. case V4L2_CID_EXPOSURE_AUTO:
  1319. s5k5baf_hw_set_auto_exposure(state, ctrl->val);
  1320. break;
  1321. case V4L2_CID_HFLIP:
  1322. s5k5baf_hw_set_mirror(state);
  1323. break;
  1324. case V4L2_CID_POWER_LINE_FREQUENCY:
  1325. s5k5baf_hw_set_anti_flicker(state, ctrl->val);
  1326. break;
  1327. case V4L2_CID_SATURATION:
  1328. s5k5baf_write(state, REG_USER_SATURATION, ctrl->val);
  1329. break;
  1330. case V4L2_CID_SHARPNESS:
  1331. s5k5baf_write(state, REG_USER_SHARPBLUR, ctrl->val);
  1332. break;
  1333. case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
  1334. s5k5baf_write(state, REG_P_COLORTEMP(0), ctrl->val);
  1335. if (state->apply_cfg)
  1336. s5k5baf_hw_sync_cfg(state);
  1337. break;
  1338. case V4L2_CID_TEST_PATTERN:
  1339. s5k5baf_hw_set_test_pattern(state, ctrl->val);
  1340. break;
  1341. }
  1342. unlock:
  1343. ret = s5k5baf_clear_error(state);
  1344. mutex_unlock(&state->lock);
  1345. return ret;
  1346. }
  1347. static const struct v4l2_ctrl_ops s5k5baf_ctrl_ops = {
  1348. .s_ctrl = s5k5baf_s_ctrl,
  1349. };
  1350. static const char * const s5k5baf_test_pattern_menu[] = {
  1351. "Disabled",
  1352. "Blank",
  1353. "Bars",
  1354. "Gradients",
  1355. "Textile",
  1356. "Textile2",
  1357. "Squares"
  1358. };
  1359. static int s5k5baf_initialize_ctrls(struct s5k5baf *state)
  1360. {
  1361. const struct v4l2_ctrl_ops *ops = &s5k5baf_ctrl_ops;
  1362. struct s5k5baf_ctrls *ctrls = &state->ctrls;
  1363. struct v4l2_ctrl_handler *hdl = &ctrls->handler;
  1364. int ret;
  1365. ret = v4l2_ctrl_handler_init(hdl, 16);
  1366. if (ret < 0) {
  1367. v4l2_err(&state->sd, "cannot init ctrl handler (%d)\n", ret);
  1368. return ret;
  1369. }
  1370. /* Auto white balance cluster */
  1371. ctrls->awb = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTO_WHITE_BALANCE,
  1372. 0, 1, 1, 1);
  1373. ctrls->gain_red = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
  1374. 0, 255, 1, S5K5BAF_GAIN_RED_DEF);
  1375. ctrls->gain_blue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
  1376. 0, 255, 1, S5K5BAF_GAIN_BLUE_DEF);
  1377. v4l2_ctrl_auto_cluster(3, &ctrls->awb, 0, false);
  1378. ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  1379. ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  1380. v4l2_ctrl_cluster(2, &ctrls->hflip);
  1381. ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
  1382. V4L2_CID_EXPOSURE_AUTO,
  1383. V4L2_EXPOSURE_MANUAL, 0, V4L2_EXPOSURE_AUTO);
  1384. /* Exposure time: x 1 us */
  1385. ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
  1386. 0, 6000000U, 1, 100000U);
  1387. /* Total gain: 256 <=> 1x */
  1388. ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
  1389. 0, 256, 1, 256);
  1390. v4l2_ctrl_auto_cluster(3, &ctrls->auto_exp, 0, false);
  1391. v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_POWER_LINE_FREQUENCY,
  1392. V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
  1393. V4L2_CID_POWER_LINE_FREQUENCY_AUTO);
  1394. v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_COLORFX,
  1395. V4L2_COLORFX_SKY_BLUE, ~0x6f, V4L2_COLORFX_NONE);
  1396. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_WHITE_BALANCE_TEMPERATURE,
  1397. 0, 256, 1, 0);
  1398. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, -127, 127, 1, 0);
  1399. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS, -127, 127, 1, 0);
  1400. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, -127, 127, 1, 0);
  1401. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SHARPNESS, -127, 127, 1, 0);
  1402. v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
  1403. ARRAY_SIZE(s5k5baf_test_pattern_menu) - 1,
  1404. 0, 0, s5k5baf_test_pattern_menu);
  1405. if (hdl->error) {
  1406. v4l2_err(&state->sd, "error creating controls (%d)\n",
  1407. hdl->error);
  1408. ret = hdl->error;
  1409. v4l2_ctrl_handler_free(hdl);
  1410. return ret;
  1411. }
  1412. state->sd.ctrl_handler = hdl;
  1413. return 0;
  1414. }
  1415. /*
  1416. * V4L2 subdev internal operations
  1417. */
  1418. static int s5k5baf_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  1419. {
  1420. struct v4l2_mbus_framefmt *mf;
  1421. mf = v4l2_subdev_get_try_format(fh, PAD_CIS);
  1422. s5k5baf_try_cis_format(mf);
  1423. if (s5k5baf_is_cis_subdev(sd))
  1424. return 0;
  1425. mf = v4l2_subdev_get_try_format(fh, PAD_OUT);
  1426. mf->colorspace = s5k5baf_formats[0].colorspace;
  1427. mf->code = s5k5baf_formats[0].code;
  1428. mf->width = s5k5baf_cis_rect.width;
  1429. mf->height = s5k5baf_cis_rect.height;
  1430. mf->field = V4L2_FIELD_NONE;
  1431. *v4l2_subdev_get_try_crop(fh, PAD_CIS) = s5k5baf_cis_rect;
  1432. *v4l2_subdev_get_try_compose(fh, PAD_CIS) = s5k5baf_cis_rect;
  1433. *v4l2_subdev_get_try_crop(fh, PAD_OUT) = s5k5baf_cis_rect;
  1434. return 0;
  1435. }
  1436. static int s5k5baf_check_fw_revision(struct s5k5baf *state)
  1437. {
  1438. u16 api_ver = 0, fw_rev = 0, s_id = 0;
  1439. int ret;
  1440. api_ver = s5k5baf_read(state, REG_FW_APIVER);
  1441. fw_rev = s5k5baf_read(state, REG_FW_REVISION) & 0xff;
  1442. s_id = s5k5baf_read(state, REG_FW_SENSOR_ID);
  1443. ret = s5k5baf_clear_error(state);
  1444. if (ret < 0)
  1445. return ret;
  1446. v4l2_info(&state->sd, "FW API=%#x, revision=%#x sensor_id=%#x\n",
  1447. api_ver, fw_rev, s_id);
  1448. if (api_ver != S5K5BAF_FW_APIVER) {
  1449. v4l2_err(&state->sd, "FW API version not supported\n");
  1450. return -ENODEV;
  1451. }
  1452. return 0;
  1453. }
  1454. static int s5k5baf_registered(struct v4l2_subdev *sd)
  1455. {
  1456. struct s5k5baf *state = to_s5k5baf(sd);
  1457. int ret;
  1458. ret = v4l2_device_register_subdev(sd->v4l2_dev, &state->cis_sd);
  1459. if (ret < 0)
  1460. v4l2_err(sd, "failed to register subdev %s\n",
  1461. state->cis_sd.name);
  1462. else
  1463. ret = media_entity_create_link(&state->cis_sd.entity, PAD_CIS,
  1464. &state->sd.entity, PAD_CIS,
  1465. MEDIA_LNK_FL_IMMUTABLE |
  1466. MEDIA_LNK_FL_ENABLED);
  1467. return ret;
  1468. }
  1469. static void s5k5baf_unregistered(struct v4l2_subdev *sd)
  1470. {
  1471. struct s5k5baf *state = to_s5k5baf(sd);
  1472. v4l2_device_unregister_subdev(&state->cis_sd);
  1473. }
  1474. static const struct v4l2_subdev_ops s5k5baf_cis_subdev_ops = {
  1475. .pad = &s5k5baf_cis_pad_ops,
  1476. };
  1477. static const struct v4l2_subdev_internal_ops s5k5baf_cis_subdev_internal_ops = {
  1478. .open = s5k5baf_open,
  1479. };
  1480. static const struct v4l2_subdev_internal_ops s5k5baf_subdev_internal_ops = {
  1481. .registered = s5k5baf_registered,
  1482. .unregistered = s5k5baf_unregistered,
  1483. .open = s5k5baf_open,
  1484. };
  1485. static const struct v4l2_subdev_core_ops s5k5baf_core_ops = {
  1486. .s_power = s5k5baf_set_power,
  1487. .log_status = v4l2_ctrl_subdev_log_status,
  1488. };
  1489. static const struct v4l2_subdev_ops s5k5baf_subdev_ops = {
  1490. .core = &s5k5baf_core_ops,
  1491. .pad = &s5k5baf_pad_ops,
  1492. .video = &s5k5baf_video_ops,
  1493. };
  1494. static int s5k5baf_configure_gpios(struct s5k5baf *state)
  1495. {
  1496. static const char const *name[] = { "S5K5BAF_STBY", "S5K5BAF_RST" };
  1497. struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
  1498. struct s5k5baf_gpio *g = state->gpios;
  1499. int ret, i;
  1500. for (i = 0; i < NUM_GPIOS; ++i) {
  1501. int flags = GPIOF_DIR_OUT;
  1502. if (g[i].level)
  1503. flags |= GPIOF_INIT_HIGH;
  1504. ret = devm_gpio_request_one(&c->dev, g[i].gpio, flags, name[i]);
  1505. if (ret < 0) {
  1506. v4l2_err(c, "failed to request gpio %s\n", name[i]);
  1507. return ret;
  1508. }
  1509. }
  1510. return 0;
  1511. }
  1512. static int s5k5baf_parse_gpios(struct s5k5baf_gpio *gpios, struct device *dev)
  1513. {
  1514. static const char * const names[] = {
  1515. "stbyn-gpios",
  1516. "rstn-gpios",
  1517. };
  1518. struct device_node *node = dev->of_node;
  1519. enum of_gpio_flags flags;
  1520. int ret, i;
  1521. for (i = 0; i < NUM_GPIOS; ++i) {
  1522. ret = of_get_named_gpio_flags(node, names[i], 0, &flags);
  1523. if (ret < 0) {
  1524. dev_err(dev, "no %s GPIO pin provided\n", names[i]);
  1525. return ret;
  1526. }
  1527. gpios[i].gpio = ret;
  1528. gpios[i].level = !(flags & OF_GPIO_ACTIVE_LOW);
  1529. }
  1530. return 0;
  1531. }
  1532. static int s5k5baf_parse_device_node(struct s5k5baf *state, struct device *dev)
  1533. {
  1534. struct device_node *node = dev->of_node;
  1535. struct device_node *node_ep;
  1536. struct v4l2_of_endpoint ep;
  1537. int ret;
  1538. if (!node) {
  1539. dev_err(dev, "no device-tree node provided\n");
  1540. return -EINVAL;
  1541. }
  1542. ret = of_property_read_u32(node, "clock-frequency",
  1543. &state->mclk_frequency);
  1544. if (ret < 0) {
  1545. state->mclk_frequency = S5K5BAF_DEFAULT_MCLK_FREQ;
  1546. dev_info(dev, "using default %u Hz clock frequency\n",
  1547. state->mclk_frequency);
  1548. }
  1549. ret = s5k5baf_parse_gpios(state->gpios, dev);
  1550. if (ret < 0)
  1551. return ret;
  1552. node_ep = of_graph_get_next_endpoint(node, NULL);
  1553. if (!node_ep) {
  1554. dev_err(dev, "no endpoint defined at node %s\n",
  1555. node->full_name);
  1556. return -EINVAL;
  1557. }
  1558. v4l2_of_parse_endpoint(node_ep, &ep);
  1559. of_node_put(node_ep);
  1560. state->bus_type = ep.bus_type;
  1561. switch (state->bus_type) {
  1562. case V4L2_MBUS_CSI2:
  1563. state->nlanes = ep.bus.mipi_csi2.num_data_lanes;
  1564. break;
  1565. case V4L2_MBUS_PARALLEL:
  1566. break;
  1567. default:
  1568. dev_err(dev, "unsupported bus in endpoint defined at node %s\n",
  1569. node->full_name);
  1570. return -EINVAL;
  1571. }
  1572. return 0;
  1573. }
  1574. static int s5k5baf_configure_subdevs(struct s5k5baf *state,
  1575. struct i2c_client *c)
  1576. {
  1577. struct v4l2_subdev *sd;
  1578. int ret;
  1579. sd = &state->cis_sd;
  1580. v4l2_subdev_init(sd, &s5k5baf_cis_subdev_ops);
  1581. sd->owner = THIS_MODULE;
  1582. v4l2_set_subdevdata(sd, state);
  1583. snprintf(sd->name, sizeof(sd->name), "S5K5BAF-CIS %d-%04x",
  1584. i2c_adapter_id(c->adapter), c->addr);
  1585. sd->internal_ops = &s5k5baf_cis_subdev_internal_ops;
  1586. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1587. state->cis_pad.flags = MEDIA_PAD_FL_SOURCE;
  1588. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
  1589. ret = media_entity_init(&sd->entity, NUM_CIS_PADS, &state->cis_pad, 0);
  1590. if (ret < 0)
  1591. goto err;
  1592. sd = &state->sd;
  1593. v4l2_i2c_subdev_init(sd, c, &s5k5baf_subdev_ops);
  1594. snprintf(sd->name, sizeof(sd->name), "S5K5BAF-ISP %d-%04x",
  1595. i2c_adapter_id(c->adapter), c->addr);
  1596. sd->internal_ops = &s5k5baf_subdev_internal_ops;
  1597. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1598. state->pads[PAD_CIS].flags = MEDIA_PAD_FL_SINK;
  1599. state->pads[PAD_OUT].flags = MEDIA_PAD_FL_SOURCE;
  1600. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
  1601. ret = media_entity_init(&sd->entity, NUM_ISP_PADS, state->pads, 0);
  1602. if (!ret)
  1603. return 0;
  1604. media_entity_cleanup(&state->cis_sd.entity);
  1605. err:
  1606. dev_err(&c->dev, "cannot init media entity %s\n", sd->name);
  1607. return ret;
  1608. }
  1609. static int s5k5baf_configure_regulators(struct s5k5baf *state)
  1610. {
  1611. struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
  1612. int ret;
  1613. int i;
  1614. for (i = 0; i < S5K5BAF_NUM_SUPPLIES; i++)
  1615. state->supplies[i].supply = s5k5baf_supply_names[i];
  1616. ret = devm_regulator_bulk_get(&c->dev, S5K5BAF_NUM_SUPPLIES,
  1617. state->supplies);
  1618. if (ret < 0)
  1619. v4l2_err(c, "failed to get regulators\n");
  1620. return ret;
  1621. }
  1622. static int s5k5baf_probe(struct i2c_client *c,
  1623. const struct i2c_device_id *id)
  1624. {
  1625. struct s5k5baf *state;
  1626. int ret;
  1627. state = devm_kzalloc(&c->dev, sizeof(*state), GFP_KERNEL);
  1628. if (!state)
  1629. return -ENOMEM;
  1630. mutex_init(&state->lock);
  1631. state->crop_sink = s5k5baf_cis_rect;
  1632. state->compose = s5k5baf_cis_rect;
  1633. state->crop_source = s5k5baf_cis_rect;
  1634. ret = s5k5baf_parse_device_node(state, &c->dev);
  1635. if (ret < 0)
  1636. return ret;
  1637. ret = s5k5baf_configure_subdevs(state, c);
  1638. if (ret < 0)
  1639. return ret;
  1640. ret = s5k5baf_configure_gpios(state);
  1641. if (ret < 0)
  1642. goto err_me;
  1643. ret = s5k5baf_configure_regulators(state);
  1644. if (ret < 0)
  1645. goto err_me;
  1646. state->clock = devm_clk_get(state->sd.dev, S5K5BAF_CLK_NAME);
  1647. if (IS_ERR(state->clock)) {
  1648. ret = -EPROBE_DEFER;
  1649. goto err_me;
  1650. }
  1651. ret = s5k5baf_power_on(state);
  1652. if (ret < 0) {
  1653. ret = -EPROBE_DEFER;
  1654. goto err_me;
  1655. }
  1656. s5k5baf_hw_init(state);
  1657. ret = s5k5baf_check_fw_revision(state);
  1658. s5k5baf_power_off(state);
  1659. if (ret < 0)
  1660. goto err_me;
  1661. ret = s5k5baf_initialize_ctrls(state);
  1662. if (ret < 0)
  1663. goto err_me;
  1664. ret = v4l2_async_register_subdev(&state->sd);
  1665. if (ret < 0)
  1666. goto err_ctrl;
  1667. return 0;
  1668. err_ctrl:
  1669. v4l2_ctrl_handler_free(state->sd.ctrl_handler);
  1670. err_me:
  1671. media_entity_cleanup(&state->sd.entity);
  1672. media_entity_cleanup(&state->cis_sd.entity);
  1673. return ret;
  1674. }
  1675. static int s5k5baf_remove(struct i2c_client *c)
  1676. {
  1677. struct v4l2_subdev *sd = i2c_get_clientdata(c);
  1678. struct s5k5baf *state = to_s5k5baf(sd);
  1679. v4l2_async_unregister_subdev(sd);
  1680. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1681. media_entity_cleanup(&sd->entity);
  1682. sd = &state->cis_sd;
  1683. v4l2_device_unregister_subdev(sd);
  1684. media_entity_cleanup(&sd->entity);
  1685. return 0;
  1686. }
  1687. static const struct i2c_device_id s5k5baf_id[] = {
  1688. { S5K5BAF_DRIVER_NAME, 0 },
  1689. { },
  1690. };
  1691. MODULE_DEVICE_TABLE(i2c, s5k5baf_id);
  1692. static const struct of_device_id s5k5baf_of_match[] = {
  1693. { .compatible = "samsung,s5k5baf" },
  1694. { }
  1695. };
  1696. MODULE_DEVICE_TABLE(of, s5k5baf_of_match);
  1697. static struct i2c_driver s5k5baf_i2c_driver = {
  1698. .driver = {
  1699. .of_match_table = s5k5baf_of_match,
  1700. .name = S5K5BAF_DRIVER_NAME
  1701. },
  1702. .probe = s5k5baf_probe,
  1703. .remove = s5k5baf_remove,
  1704. .id_table = s5k5baf_id,
  1705. };
  1706. module_i2c_driver(s5k5baf_i2c_driver);
  1707. MODULE_DESCRIPTION("Samsung S5K5BAF(X) UXGA camera driver");
  1708. MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
  1709. MODULE_LICENSE("GPL v2");