s5k5baf.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059
  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. u32 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. { MEDIA_BUS_FMT_VYUY8_2X8, V4L2_COLORSPACE_JPEG, 5 },
  295. /* range 16-240 */
  296. { MEDIA_BUS_FMT_VYUY8_2X8, V4L2_COLORSPACE_REC709, 6 },
  297. { MEDIA_BUS_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 __le16 *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. if (!d)
  332. return -ENOMEM;
  333. for (i = 0; i < count; ++i)
  334. d[i] = le16_to_cpu(data[i]);
  335. f = (struct s5k5baf_fw *)d;
  336. if (count < 1 + 2 * f->count) {
  337. dev_err(dev, "invalid firmware header (count=%d size=%zu)\n",
  338. f->count, 2 * (count + S5K5BAG_FW_TAG_LEN));
  339. return -EINVAL;
  340. }
  341. end = d + count;
  342. d += 1 + 2 * f->count;
  343. for (i = 0; i < f->count; ++i) {
  344. if (f->seq[i].offset + d <= end)
  345. continue;
  346. dev_err(dev, "invalid firmware header (seq=%d)\n", i);
  347. return -EINVAL;
  348. }
  349. *fw = f;
  350. return 0;
  351. }
  352. static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
  353. {
  354. return &container_of(ctrl->handler, struct s5k5baf, ctrls.handler)->sd;
  355. }
  356. static inline bool s5k5baf_is_cis_subdev(struct v4l2_subdev *sd)
  357. {
  358. return sd->entity.type == MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
  359. }
  360. static inline struct s5k5baf *to_s5k5baf(struct v4l2_subdev *sd)
  361. {
  362. if (s5k5baf_is_cis_subdev(sd))
  363. return container_of(sd, struct s5k5baf, cis_sd);
  364. else
  365. return container_of(sd, struct s5k5baf, sd);
  366. }
  367. static u16 s5k5baf_i2c_read(struct s5k5baf *state, u16 addr)
  368. {
  369. struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
  370. __be16 w, r;
  371. u16 res;
  372. struct i2c_msg msg[] = {
  373. { .addr = c->addr, .flags = 0,
  374. .len = 2, .buf = (u8 *)&w },
  375. { .addr = c->addr, .flags = I2C_M_RD,
  376. .len = 2, .buf = (u8 *)&r },
  377. };
  378. int ret;
  379. if (state->error)
  380. return 0;
  381. w = cpu_to_be16(addr);
  382. ret = i2c_transfer(c->adapter, msg, 2);
  383. res = be16_to_cpu(r);
  384. v4l2_dbg(3, debug, c, "i2c_read: 0x%04x : 0x%04x\n", addr, res);
  385. if (ret != 2) {
  386. v4l2_err(c, "i2c_read: error during transfer (%d)\n", ret);
  387. state->error = ret;
  388. }
  389. return res;
  390. }
  391. static void s5k5baf_i2c_write(struct s5k5baf *state, u16 addr, u16 val)
  392. {
  393. u8 buf[4] = { addr >> 8, addr & 0xFF, val >> 8, val & 0xFF };
  394. struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
  395. int ret;
  396. if (state->error)
  397. return;
  398. ret = i2c_master_send(c, buf, 4);
  399. v4l2_dbg(3, debug, c, "i2c_write: 0x%04x : 0x%04x\n", addr, val);
  400. if (ret != 4) {
  401. v4l2_err(c, "i2c_write: error during transfer (%d)\n", ret);
  402. state->error = ret;
  403. }
  404. }
  405. static u16 s5k5baf_read(struct s5k5baf *state, u16 addr)
  406. {
  407. s5k5baf_i2c_write(state, REG_CMDRD_ADDR, addr);
  408. return s5k5baf_i2c_read(state, REG_CMD_BUF);
  409. }
  410. static void s5k5baf_write(struct s5k5baf *state, u16 addr, u16 val)
  411. {
  412. s5k5baf_i2c_write(state, REG_CMDWR_ADDR, addr);
  413. s5k5baf_i2c_write(state, REG_CMD_BUF, val);
  414. }
  415. static void s5k5baf_write_arr_seq(struct s5k5baf *state, u16 addr,
  416. u16 count, const u16 *seq)
  417. {
  418. struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
  419. __be16 buf[65];
  420. s5k5baf_i2c_write(state, REG_CMDWR_ADDR, addr);
  421. if (state->error)
  422. return;
  423. v4l2_dbg(3, debug, c, "i2c_write_seq(count=%d): %*ph\n", count,
  424. min(2 * count, 64), seq);
  425. buf[0] = cpu_to_be16(REG_CMD_BUF);
  426. while (count > 0) {
  427. int n = min_t(int, count, ARRAY_SIZE(buf) - 1);
  428. int ret, i;
  429. for (i = 1; i <= n; ++i)
  430. buf[i] = cpu_to_be16(*seq++);
  431. i *= 2;
  432. ret = i2c_master_send(c, (char *)buf, i);
  433. if (ret != i) {
  434. v4l2_err(c, "i2c_write_seq: error during transfer (%d)\n", ret);
  435. state->error = ret;
  436. break;
  437. }
  438. count -= n;
  439. }
  440. }
  441. #define s5k5baf_write_seq(state, addr, seq...) \
  442. s5k5baf_write_arr_seq(state, addr, sizeof((char[]){ seq }), \
  443. (const u16 []){ seq });
  444. /* add items count at the beginning of the list */
  445. #define NSEQ(seq...) sizeof((char[]){ seq }), seq
  446. /*
  447. * s5k5baf_write_nseq() - Writes sequences of values to sensor memory via i2c
  448. * @nseq: sequence of u16 words in format:
  449. * (N, address, value[1]...value[N-1])*,0
  450. * Ex.:
  451. * u16 seq[] = { NSEQ(0x4000, 1, 1), NSEQ(0x4010, 640, 480), 0 };
  452. * ret = s5k5baf_write_nseq(c, seq);
  453. */
  454. static void s5k5baf_write_nseq(struct s5k5baf *state, const u16 *nseq)
  455. {
  456. int count;
  457. while ((count = *nseq++)) {
  458. u16 addr = *nseq++;
  459. --count;
  460. s5k5baf_write_arr_seq(state, addr, count, nseq);
  461. nseq += count;
  462. }
  463. }
  464. static void s5k5baf_synchronize(struct s5k5baf *state, int timeout, u16 addr)
  465. {
  466. unsigned long end = jiffies + msecs_to_jiffies(timeout);
  467. u16 reg;
  468. s5k5baf_write(state, addr, 1);
  469. do {
  470. reg = s5k5baf_read(state, addr);
  471. if (state->error || !reg)
  472. return;
  473. usleep_range(5000, 10000);
  474. } while (time_is_after_jiffies(end));
  475. v4l2_err(&state->sd, "timeout on register synchronize (%#x)\n", addr);
  476. state->error = -ETIMEDOUT;
  477. }
  478. static u16 *s5k5baf_fw_get_seq(struct s5k5baf *state, u16 seq_id)
  479. {
  480. struct s5k5baf_fw *fw = state->fw;
  481. u16 *data;
  482. int i;
  483. if (fw == NULL)
  484. return NULL;
  485. data = fw->data + 2 * fw->count;
  486. for (i = 0; i < fw->count; ++i) {
  487. if (fw->seq[i].id == seq_id)
  488. return data + fw->seq[i].offset;
  489. }
  490. return NULL;
  491. }
  492. static void s5k5baf_hw_patch(struct s5k5baf *state)
  493. {
  494. u16 *seq = s5k5baf_fw_get_seq(state, S5K5BAF_FW_ID_PATCH);
  495. if (seq)
  496. s5k5baf_write_nseq(state, seq);
  497. }
  498. static void s5k5baf_hw_set_clocks(struct s5k5baf *state)
  499. {
  500. unsigned long mclk = state->mclk_frequency / 1000;
  501. u16 status;
  502. static const u16 nseq_clk_cfg[] = {
  503. NSEQ(REG_I_USE_NPVI_CLOCKS,
  504. NPVI_CLOCKS, NMIPI_CLOCKS, 0,
  505. SCLK_PVI_FREQ / 4, PCLK_MIN_FREQ / 4, PCLK_MAX_FREQ / 4,
  506. SCLK_MIPI_FREQ / 4, PCLK_MIN_FREQ / 4, PCLK_MAX_FREQ / 4),
  507. NSEQ(REG_I_USE_REGS_API, 1),
  508. 0
  509. };
  510. s5k5baf_write_seq(state, REG_I_INCLK_FREQ_L, mclk & 0xffff, mclk >> 16);
  511. s5k5baf_write_nseq(state, nseq_clk_cfg);
  512. s5k5baf_synchronize(state, 250, REG_I_INIT_PARAMS_UPDATED);
  513. status = s5k5baf_read(state, REG_I_ERROR_INFO);
  514. if (!state->error && status) {
  515. v4l2_err(&state->sd, "error configuring PLL (%d)\n", status);
  516. state->error = -EINVAL;
  517. }
  518. }
  519. /* set custom color correction matrices for various illuminations */
  520. static void s5k5baf_hw_set_ccm(struct s5k5baf *state)
  521. {
  522. u16 *seq = s5k5baf_fw_get_seq(state, S5K5BAF_FW_ID_CCM);
  523. if (seq)
  524. s5k5baf_write_nseq(state, seq);
  525. }
  526. /* CIS sensor tuning, based on undocumented android driver code */
  527. static void s5k5baf_hw_set_cis(struct s5k5baf *state)
  528. {
  529. u16 *seq = s5k5baf_fw_get_seq(state, S5K5BAF_FW_ID_CIS);
  530. if (!seq)
  531. return;
  532. s5k5baf_i2c_write(state, REG_CMDWR_PAGE, PAGE_IF_HW);
  533. s5k5baf_write_nseq(state, seq);
  534. s5k5baf_i2c_write(state, REG_CMDWR_PAGE, PAGE_IF_SW);
  535. }
  536. static void s5k5baf_hw_sync_cfg(struct s5k5baf *state)
  537. {
  538. s5k5baf_write(state, REG_G_PREV_CFG_CHG, 1);
  539. if (state->apply_crop) {
  540. s5k5baf_write(state, REG_G_INPUTS_CHANGE_REQ, 1);
  541. s5k5baf_write(state, REG_G_PREV_CFG_BYPASS_CHANGED, 1);
  542. }
  543. s5k5baf_synchronize(state, 500, REG_G_NEW_CFG_SYNC);
  544. }
  545. /* Set horizontal and vertical image flipping */
  546. static void s5k5baf_hw_set_mirror(struct s5k5baf *state)
  547. {
  548. u16 flip = state->ctrls.vflip->val | (state->ctrls.vflip->val << 1);
  549. s5k5baf_write(state, REG_P_PREV_MIRROR(0), flip);
  550. if (state->streaming)
  551. s5k5baf_hw_sync_cfg(state);
  552. }
  553. static void s5k5baf_hw_set_alg(struct s5k5baf *state, u16 alg, bool enable)
  554. {
  555. u16 cur_alg, new_alg;
  556. if (!state->valid_auto_alg)
  557. cur_alg = s5k5baf_read(state, REG_DBG_AUTOALG_EN);
  558. else
  559. cur_alg = state->auto_alg;
  560. new_alg = enable ? (cur_alg | alg) : (cur_alg & ~alg);
  561. if (new_alg != cur_alg)
  562. s5k5baf_write(state, REG_DBG_AUTOALG_EN, new_alg);
  563. if (state->error)
  564. return;
  565. state->valid_auto_alg = 1;
  566. state->auto_alg = new_alg;
  567. }
  568. /* Configure auto/manual white balance and R/G/B gains */
  569. static void s5k5baf_hw_set_awb(struct s5k5baf *state, int awb)
  570. {
  571. struct s5k5baf_ctrls *ctrls = &state->ctrls;
  572. if (!awb)
  573. s5k5baf_write_seq(state, REG_SF_RGAIN,
  574. ctrls->gain_red->val, 1,
  575. S5K5BAF_GAIN_GREEN_DEF, 1,
  576. ctrls->gain_blue->val, 1,
  577. 1);
  578. s5k5baf_hw_set_alg(state, AALG_WB_EN, awb);
  579. }
  580. /* Program FW with exposure time, 'exposure' in us units */
  581. static void s5k5baf_hw_set_user_exposure(struct s5k5baf *state, int exposure)
  582. {
  583. unsigned int time = exposure / 10;
  584. s5k5baf_write_seq(state, REG_SF_USR_EXPOSURE_L,
  585. time & 0xffff, time >> 16, 1);
  586. }
  587. static void s5k5baf_hw_set_user_gain(struct s5k5baf *state, int gain)
  588. {
  589. s5k5baf_write_seq(state, REG_SF_USR_TOT_GAIN, gain, 1);
  590. }
  591. /* Set auto/manual exposure and total gain */
  592. static void s5k5baf_hw_set_auto_exposure(struct s5k5baf *state, int value)
  593. {
  594. if (value == V4L2_EXPOSURE_AUTO) {
  595. s5k5baf_hw_set_alg(state, AALG_AE_EN | AALG_DIVLEI_EN, true);
  596. } else {
  597. unsigned int exp_time = state->ctrls.exposure->val;
  598. s5k5baf_hw_set_user_exposure(state, exp_time);
  599. s5k5baf_hw_set_user_gain(state, state->ctrls.gain->val);
  600. s5k5baf_hw_set_alg(state, AALG_AE_EN | AALG_DIVLEI_EN, false);
  601. }
  602. }
  603. static void s5k5baf_hw_set_anti_flicker(struct s5k5baf *state, int v)
  604. {
  605. if (v == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) {
  606. s5k5baf_hw_set_alg(state, AALG_FLICKER_EN, true);
  607. } else {
  608. /* The V4L2_CID_LINE_FREQUENCY control values match
  609. * the register values */
  610. s5k5baf_write_seq(state, REG_SF_FLICKER_QUANT, v, 1);
  611. s5k5baf_hw_set_alg(state, AALG_FLICKER_EN, false);
  612. }
  613. }
  614. static void s5k5baf_hw_set_colorfx(struct s5k5baf *state, int val)
  615. {
  616. static const u16 colorfx[] = {
  617. [V4L2_COLORFX_NONE] = 0,
  618. [V4L2_COLORFX_BW] = 1,
  619. [V4L2_COLORFX_NEGATIVE] = 2,
  620. [V4L2_COLORFX_SEPIA] = 3,
  621. [V4L2_COLORFX_SKY_BLUE] = 4,
  622. [V4L2_COLORFX_SKETCH] = 5,
  623. };
  624. s5k5baf_write(state, REG_G_SPEC_EFFECTS, colorfx[val]);
  625. }
  626. static int s5k5baf_find_pixfmt(struct v4l2_mbus_framefmt *mf)
  627. {
  628. int i, c = -1;
  629. for (i = 0; i < ARRAY_SIZE(s5k5baf_formats); i++) {
  630. if (mf->colorspace != s5k5baf_formats[i].colorspace)
  631. continue;
  632. if (mf->code == s5k5baf_formats[i].code)
  633. return i;
  634. if (c < 0)
  635. c = i;
  636. }
  637. return (c < 0) ? 0 : c;
  638. }
  639. static int s5k5baf_clear_error(struct s5k5baf *state)
  640. {
  641. int ret = state->error;
  642. state->error = 0;
  643. return ret;
  644. }
  645. static int s5k5baf_hw_set_video_bus(struct s5k5baf *state)
  646. {
  647. u16 en_pkts;
  648. if (state->bus_type == V4L2_MBUS_CSI2)
  649. en_pkts = EN_PACKETS_CSI2;
  650. else
  651. en_pkts = 0;
  652. s5k5baf_write_seq(state, REG_OIF_EN_MIPI_LANES,
  653. state->nlanes, en_pkts, 1);
  654. return s5k5baf_clear_error(state);
  655. }
  656. static u16 s5k5baf_get_cfg_error(struct s5k5baf *state)
  657. {
  658. u16 err = s5k5baf_read(state, REG_G_PREV_CFG_ERROR);
  659. if (err)
  660. s5k5baf_write(state, REG_G_PREV_CFG_ERROR, 0);
  661. return err;
  662. }
  663. static void s5k5baf_hw_set_fiv(struct s5k5baf *state, u16 fiv)
  664. {
  665. s5k5baf_write(state, REG_P_MAX_FR_TIME(0), fiv);
  666. s5k5baf_hw_sync_cfg(state);
  667. }
  668. static void s5k5baf_hw_find_min_fiv(struct s5k5baf *state)
  669. {
  670. u16 err, fiv;
  671. int n;
  672. fiv = s5k5baf_read(state, REG_G_ACTUAL_P_FR_TIME);
  673. if (state->error)
  674. return;
  675. for (n = 5; n > 0; --n) {
  676. s5k5baf_hw_set_fiv(state, fiv);
  677. err = s5k5baf_get_cfg_error(state);
  678. if (state->error)
  679. return;
  680. switch (err) {
  681. case CFG_ERROR_RANGE:
  682. ++fiv;
  683. break;
  684. case 0:
  685. state->fiv = fiv;
  686. v4l2_info(&state->sd,
  687. "found valid frame interval: %d00us\n", fiv);
  688. return;
  689. default:
  690. v4l2_err(&state->sd,
  691. "error setting frame interval: %d\n", err);
  692. state->error = -EINVAL;
  693. }
  694. }
  695. v4l2_err(&state->sd, "cannot find correct frame interval\n");
  696. state->error = -ERANGE;
  697. }
  698. static void s5k5baf_hw_validate_cfg(struct s5k5baf *state)
  699. {
  700. u16 err;
  701. err = s5k5baf_get_cfg_error(state);
  702. if (state->error)
  703. return;
  704. switch (err) {
  705. case 0:
  706. state->apply_cfg = 1;
  707. return;
  708. case CFG_ERROR_RANGE:
  709. s5k5baf_hw_find_min_fiv(state);
  710. if (!state->error)
  711. state->apply_cfg = 1;
  712. return;
  713. default:
  714. v4l2_err(&state->sd,
  715. "error setting format: %d\n", err);
  716. state->error = -EINVAL;
  717. }
  718. }
  719. static void s5k5baf_rescale(struct v4l2_rect *r, const struct v4l2_rect *v,
  720. const struct v4l2_rect *n,
  721. const struct v4l2_rect *d)
  722. {
  723. r->left = v->left * n->width / d->width;
  724. r->top = v->top * n->height / d->height;
  725. r->width = v->width * n->width / d->width;
  726. r->height = v->height * n->height / d->height;
  727. }
  728. static int s5k5baf_hw_set_crop_rects(struct s5k5baf *state)
  729. {
  730. struct v4l2_rect *p, r;
  731. u16 err;
  732. int ret;
  733. p = &state->crop_sink;
  734. s5k5baf_write_seq(state, REG_G_PREVREQ_IN_WIDTH, p->width, p->height,
  735. p->left, p->top);
  736. s5k5baf_rescale(&r, &state->crop_source, &state->crop_sink,
  737. &state->compose);
  738. s5k5baf_write_seq(state, REG_G_PREVZOOM_IN_WIDTH, r.width, r.height,
  739. r.left, r.top);
  740. s5k5baf_synchronize(state, 500, REG_G_INPUTS_CHANGE_REQ);
  741. s5k5baf_synchronize(state, 500, REG_G_PREV_CFG_BYPASS_CHANGED);
  742. err = s5k5baf_get_cfg_error(state);
  743. ret = s5k5baf_clear_error(state);
  744. if (ret < 0)
  745. return ret;
  746. switch (err) {
  747. case 0:
  748. break;
  749. case CFG_ERROR_RANGE:
  750. /* retry crop with frame interval set to max */
  751. s5k5baf_hw_set_fiv(state, S5K5BAF_MAX_FR_TIME);
  752. err = s5k5baf_get_cfg_error(state);
  753. ret = s5k5baf_clear_error(state);
  754. if (ret < 0)
  755. return ret;
  756. if (err) {
  757. v4l2_err(&state->sd,
  758. "crop error on max frame interval: %d\n", err);
  759. state->error = -EINVAL;
  760. }
  761. s5k5baf_hw_set_fiv(state, state->req_fiv);
  762. s5k5baf_hw_validate_cfg(state);
  763. break;
  764. default:
  765. v4l2_err(&state->sd, "crop error: %d\n", err);
  766. return -EINVAL;
  767. }
  768. if (!state->apply_cfg)
  769. return 0;
  770. p = &state->crop_source;
  771. s5k5baf_write_seq(state, REG_P_OUT_WIDTH(0), p->width, p->height);
  772. s5k5baf_hw_set_fiv(state, state->req_fiv);
  773. s5k5baf_hw_validate_cfg(state);
  774. return s5k5baf_clear_error(state);
  775. }
  776. static void s5k5baf_hw_set_config(struct s5k5baf *state)
  777. {
  778. u16 reg_fmt = s5k5baf_formats[state->pixfmt].reg_p_fmt;
  779. struct v4l2_rect *r = &state->crop_source;
  780. s5k5baf_write_seq(state, REG_P_OUT_WIDTH(0),
  781. r->width, r->height, reg_fmt,
  782. PCLK_MAX_FREQ >> 2, PCLK_MIN_FREQ >> 2,
  783. PVI_MASK_MIPI, CLK_MIPI_INDEX,
  784. FR_RATE_FIXED, FR_RATE_Q_DYNAMIC,
  785. state->req_fiv, S5K5BAF_MIN_FR_TIME);
  786. s5k5baf_hw_sync_cfg(state);
  787. s5k5baf_hw_validate_cfg(state);
  788. }
  789. static void s5k5baf_hw_set_test_pattern(struct s5k5baf *state, int id)
  790. {
  791. s5k5baf_i2c_write(state, REG_PATTERN_WIDTH, 800);
  792. s5k5baf_i2c_write(state, REG_PATTERN_HEIGHT, 511);
  793. s5k5baf_i2c_write(state, REG_PATTERN_PARAM, 0);
  794. s5k5baf_i2c_write(state, REG_PATTERN_SET, id);
  795. }
  796. static void s5k5baf_gpio_assert(struct s5k5baf *state, int id)
  797. {
  798. struct s5k5baf_gpio *gpio = &state->gpios[id];
  799. gpio_set_value(gpio->gpio, gpio->level);
  800. }
  801. static void s5k5baf_gpio_deassert(struct s5k5baf *state, int id)
  802. {
  803. struct s5k5baf_gpio *gpio = &state->gpios[id];
  804. gpio_set_value(gpio->gpio, !gpio->level);
  805. }
  806. static int s5k5baf_power_on(struct s5k5baf *state)
  807. {
  808. int ret;
  809. ret = regulator_bulk_enable(S5K5BAF_NUM_SUPPLIES, state->supplies);
  810. if (ret < 0)
  811. goto err;
  812. ret = clk_set_rate(state->clock, state->mclk_frequency);
  813. if (ret < 0)
  814. goto err_reg_dis;
  815. ret = clk_prepare_enable(state->clock);
  816. if (ret < 0)
  817. goto err_reg_dis;
  818. v4l2_dbg(1, debug, &state->sd, "clock frequency: %ld\n",
  819. clk_get_rate(state->clock));
  820. s5k5baf_gpio_deassert(state, STBY);
  821. usleep_range(50, 100);
  822. s5k5baf_gpio_deassert(state, RST);
  823. return 0;
  824. err_reg_dis:
  825. regulator_bulk_disable(S5K5BAF_NUM_SUPPLIES, state->supplies);
  826. err:
  827. v4l2_err(&state->sd, "%s() failed (%d)\n", __func__, ret);
  828. return ret;
  829. }
  830. static int s5k5baf_power_off(struct s5k5baf *state)
  831. {
  832. int ret;
  833. state->streaming = 0;
  834. state->apply_cfg = 0;
  835. state->apply_crop = 0;
  836. s5k5baf_gpio_assert(state, RST);
  837. s5k5baf_gpio_assert(state, STBY);
  838. if (!IS_ERR(state->clock))
  839. clk_disable_unprepare(state->clock);
  840. ret = regulator_bulk_disable(S5K5BAF_NUM_SUPPLIES,
  841. state->supplies);
  842. if (ret < 0)
  843. v4l2_err(&state->sd, "failed to disable regulators\n");
  844. return 0;
  845. }
  846. static void s5k5baf_hw_init(struct s5k5baf *state)
  847. {
  848. s5k5baf_i2c_write(state, AHB_MSB_ADDR_PTR, PAGE_IF_HW);
  849. s5k5baf_i2c_write(state, REG_CLEAR_HOST_INT, 0);
  850. s5k5baf_i2c_write(state, REG_SW_LOAD_COMPLETE, 1);
  851. s5k5baf_i2c_write(state, REG_CMDRD_PAGE, PAGE_IF_SW);
  852. s5k5baf_i2c_write(state, REG_CMDWR_PAGE, PAGE_IF_SW);
  853. }
  854. /*
  855. * V4L2 subdev core and video operations
  856. */
  857. static void s5k5baf_initialize_data(struct s5k5baf *state)
  858. {
  859. state->pixfmt = 0;
  860. state->req_fiv = 10000 / 15;
  861. state->fiv = state->req_fiv;
  862. state->valid_auto_alg = 0;
  863. }
  864. static int s5k5baf_load_setfile(struct s5k5baf *state)
  865. {
  866. struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
  867. const struct firmware *fw;
  868. int ret;
  869. ret = request_firmware(&fw, S5K5BAF_FW_FILENAME, &c->dev);
  870. if (ret < 0) {
  871. dev_warn(&c->dev, "firmware file (%s) not loaded\n",
  872. S5K5BAF_FW_FILENAME);
  873. return ret;
  874. }
  875. ret = s5k5baf_fw_parse(&c->dev, &state->fw, fw->size / 2,
  876. (__le16 *)fw->data);
  877. release_firmware(fw);
  878. return ret;
  879. }
  880. static int s5k5baf_set_power(struct v4l2_subdev *sd, int on)
  881. {
  882. struct s5k5baf *state = to_s5k5baf(sd);
  883. int ret = 0;
  884. mutex_lock(&state->lock);
  885. if (state->power != !on)
  886. goto out;
  887. if (on) {
  888. if (state->fw == NULL)
  889. s5k5baf_load_setfile(state);
  890. s5k5baf_initialize_data(state);
  891. ret = s5k5baf_power_on(state);
  892. if (ret < 0)
  893. goto out;
  894. s5k5baf_hw_init(state);
  895. s5k5baf_hw_patch(state);
  896. s5k5baf_i2c_write(state, REG_SET_HOST_INT, 1);
  897. s5k5baf_hw_set_clocks(state);
  898. ret = s5k5baf_hw_set_video_bus(state);
  899. if (ret < 0)
  900. goto out;
  901. s5k5baf_hw_set_cis(state);
  902. s5k5baf_hw_set_ccm(state);
  903. ret = s5k5baf_clear_error(state);
  904. if (!ret)
  905. state->power++;
  906. } else {
  907. s5k5baf_power_off(state);
  908. state->power--;
  909. }
  910. out:
  911. mutex_unlock(&state->lock);
  912. if (!ret && on)
  913. ret = v4l2_ctrl_handler_setup(&state->ctrls.handler);
  914. return ret;
  915. }
  916. static void s5k5baf_hw_set_stream(struct s5k5baf *state, int enable)
  917. {
  918. s5k5baf_write_seq(state, REG_G_ENABLE_PREV, enable, 1);
  919. }
  920. static int s5k5baf_s_stream(struct v4l2_subdev *sd, int on)
  921. {
  922. struct s5k5baf *state = to_s5k5baf(sd);
  923. int ret;
  924. mutex_lock(&state->lock);
  925. if (state->streaming == !!on) {
  926. ret = 0;
  927. goto out;
  928. }
  929. if (on) {
  930. s5k5baf_hw_set_config(state);
  931. ret = s5k5baf_hw_set_crop_rects(state);
  932. if (ret < 0)
  933. goto out;
  934. s5k5baf_hw_set_stream(state, 1);
  935. s5k5baf_i2c_write(state, 0xb0cc, 0x000b);
  936. } else {
  937. s5k5baf_hw_set_stream(state, 0);
  938. }
  939. ret = s5k5baf_clear_error(state);
  940. if (!ret)
  941. state->streaming = !state->streaming;
  942. out:
  943. mutex_unlock(&state->lock);
  944. return ret;
  945. }
  946. static int s5k5baf_g_frame_interval(struct v4l2_subdev *sd,
  947. struct v4l2_subdev_frame_interval *fi)
  948. {
  949. struct s5k5baf *state = to_s5k5baf(sd);
  950. mutex_lock(&state->lock);
  951. fi->interval.numerator = state->fiv;
  952. fi->interval.denominator = 10000;
  953. mutex_unlock(&state->lock);
  954. return 0;
  955. }
  956. static void s5k5baf_set_frame_interval(struct s5k5baf *state,
  957. struct v4l2_subdev_frame_interval *fi)
  958. {
  959. struct v4l2_fract *i = &fi->interval;
  960. if (fi->interval.denominator == 0)
  961. state->req_fiv = S5K5BAF_MAX_FR_TIME;
  962. else
  963. state->req_fiv = clamp_t(u32,
  964. i->numerator * 10000 / i->denominator,
  965. S5K5BAF_MIN_FR_TIME,
  966. S5K5BAF_MAX_FR_TIME);
  967. state->fiv = state->req_fiv;
  968. if (state->apply_cfg) {
  969. s5k5baf_hw_set_fiv(state, state->req_fiv);
  970. s5k5baf_hw_validate_cfg(state);
  971. }
  972. *i = (struct v4l2_fract){ state->fiv, 10000 };
  973. if (state->fiv == state->req_fiv)
  974. v4l2_info(&state->sd, "frame interval changed to %d00us\n",
  975. state->fiv);
  976. }
  977. static int s5k5baf_s_frame_interval(struct v4l2_subdev *sd,
  978. struct v4l2_subdev_frame_interval *fi)
  979. {
  980. struct s5k5baf *state = to_s5k5baf(sd);
  981. mutex_lock(&state->lock);
  982. s5k5baf_set_frame_interval(state, fi);
  983. mutex_unlock(&state->lock);
  984. return 0;
  985. }
  986. /*
  987. * V4L2 subdev pad level and video operations
  988. */
  989. static int s5k5baf_enum_frame_interval(struct v4l2_subdev *sd,
  990. struct v4l2_subdev_pad_config *cfg,
  991. struct v4l2_subdev_frame_interval_enum *fie)
  992. {
  993. if (fie->index > S5K5BAF_MAX_FR_TIME - S5K5BAF_MIN_FR_TIME ||
  994. fie->pad != PAD_CIS)
  995. return -EINVAL;
  996. v4l_bound_align_image(&fie->width, S5K5BAF_WIN_WIDTH_MIN,
  997. S5K5BAF_CIS_WIDTH, 1,
  998. &fie->height, S5K5BAF_WIN_HEIGHT_MIN,
  999. S5K5BAF_CIS_HEIGHT, 1, 0);
  1000. fie->interval.numerator = S5K5BAF_MIN_FR_TIME + fie->index;
  1001. fie->interval.denominator = 10000;
  1002. return 0;
  1003. }
  1004. static int s5k5baf_enum_mbus_code(struct v4l2_subdev *sd,
  1005. struct v4l2_subdev_pad_config *cfg,
  1006. struct v4l2_subdev_mbus_code_enum *code)
  1007. {
  1008. if (code->pad == PAD_CIS) {
  1009. if (code->index > 0)
  1010. return -EINVAL;
  1011. code->code = MEDIA_BUS_FMT_FIXED;
  1012. return 0;
  1013. }
  1014. if (code->index >= ARRAY_SIZE(s5k5baf_formats))
  1015. return -EINVAL;
  1016. code->code = s5k5baf_formats[code->index].code;
  1017. return 0;
  1018. }
  1019. static int s5k5baf_enum_frame_size(struct v4l2_subdev *sd,
  1020. struct v4l2_subdev_pad_config *cfg,
  1021. struct v4l2_subdev_frame_size_enum *fse)
  1022. {
  1023. int i;
  1024. if (fse->index > 0)
  1025. return -EINVAL;
  1026. if (fse->pad == PAD_CIS) {
  1027. fse->code = MEDIA_BUS_FMT_FIXED;
  1028. fse->min_width = S5K5BAF_CIS_WIDTH;
  1029. fse->max_width = S5K5BAF_CIS_WIDTH;
  1030. fse->min_height = S5K5BAF_CIS_HEIGHT;
  1031. fse->max_height = S5K5BAF_CIS_HEIGHT;
  1032. return 0;
  1033. }
  1034. i = ARRAY_SIZE(s5k5baf_formats);
  1035. while (--i)
  1036. if (fse->code == s5k5baf_formats[i].code)
  1037. break;
  1038. fse->code = s5k5baf_formats[i].code;
  1039. fse->min_width = S5K5BAF_WIN_WIDTH_MIN;
  1040. fse->max_width = S5K5BAF_CIS_WIDTH;
  1041. fse->max_height = S5K5BAF_WIN_HEIGHT_MIN;
  1042. fse->min_height = S5K5BAF_CIS_HEIGHT;
  1043. return 0;
  1044. }
  1045. static void s5k5baf_try_cis_format(struct v4l2_mbus_framefmt *mf)
  1046. {
  1047. mf->width = S5K5BAF_CIS_WIDTH;
  1048. mf->height = S5K5BAF_CIS_HEIGHT;
  1049. mf->code = MEDIA_BUS_FMT_FIXED;
  1050. mf->colorspace = V4L2_COLORSPACE_JPEG;
  1051. mf->field = V4L2_FIELD_NONE;
  1052. }
  1053. static int s5k5baf_try_isp_format(struct v4l2_mbus_framefmt *mf)
  1054. {
  1055. int pixfmt;
  1056. v4l_bound_align_image(&mf->width, S5K5BAF_WIN_WIDTH_MIN,
  1057. S5K5BAF_CIS_WIDTH, 1,
  1058. &mf->height, S5K5BAF_WIN_HEIGHT_MIN,
  1059. S5K5BAF_CIS_HEIGHT, 1, 0);
  1060. pixfmt = s5k5baf_find_pixfmt(mf);
  1061. mf->colorspace = s5k5baf_formats[pixfmt].colorspace;
  1062. mf->code = s5k5baf_formats[pixfmt].code;
  1063. mf->field = V4L2_FIELD_NONE;
  1064. return pixfmt;
  1065. }
  1066. static int s5k5baf_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
  1067. struct v4l2_subdev_format *fmt)
  1068. {
  1069. struct s5k5baf *state = to_s5k5baf(sd);
  1070. const struct s5k5baf_pixfmt *pixfmt;
  1071. struct v4l2_mbus_framefmt *mf;
  1072. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1073. mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
  1074. fmt->format = *mf;
  1075. return 0;
  1076. }
  1077. mf = &fmt->format;
  1078. if (fmt->pad == PAD_CIS) {
  1079. s5k5baf_try_cis_format(mf);
  1080. return 0;
  1081. }
  1082. mf->field = V4L2_FIELD_NONE;
  1083. mutex_lock(&state->lock);
  1084. pixfmt = &s5k5baf_formats[state->pixfmt];
  1085. mf->width = state->crop_source.width;
  1086. mf->height = state->crop_source.height;
  1087. mf->code = pixfmt->code;
  1088. mf->colorspace = pixfmt->colorspace;
  1089. mutex_unlock(&state->lock);
  1090. return 0;
  1091. }
  1092. static int s5k5baf_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
  1093. struct v4l2_subdev_format *fmt)
  1094. {
  1095. struct v4l2_mbus_framefmt *mf = &fmt->format;
  1096. struct s5k5baf *state = to_s5k5baf(sd);
  1097. const struct s5k5baf_pixfmt *pixfmt;
  1098. int ret = 0;
  1099. mf->field = V4L2_FIELD_NONE;
  1100. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1101. *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = *mf;
  1102. return 0;
  1103. }
  1104. if (fmt->pad == PAD_CIS) {
  1105. s5k5baf_try_cis_format(mf);
  1106. return 0;
  1107. }
  1108. mutex_lock(&state->lock);
  1109. if (state->streaming) {
  1110. mutex_unlock(&state->lock);
  1111. return -EBUSY;
  1112. }
  1113. state->pixfmt = s5k5baf_try_isp_format(mf);
  1114. pixfmt = &s5k5baf_formats[state->pixfmt];
  1115. mf->code = pixfmt->code;
  1116. mf->colorspace = pixfmt->colorspace;
  1117. mf->width = state->crop_source.width;
  1118. mf->height = state->crop_source.height;
  1119. mutex_unlock(&state->lock);
  1120. return ret;
  1121. }
  1122. enum selection_rect { R_CIS, R_CROP_SINK, R_COMPOSE, R_CROP_SOURCE, R_INVALID };
  1123. static enum selection_rect s5k5baf_get_sel_rect(u32 pad, u32 target)
  1124. {
  1125. switch (target) {
  1126. case V4L2_SEL_TGT_CROP_BOUNDS:
  1127. return pad ? R_COMPOSE : R_CIS;
  1128. case V4L2_SEL_TGT_CROP:
  1129. return pad ? R_CROP_SOURCE : R_CROP_SINK;
  1130. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  1131. return pad ? R_INVALID : R_CROP_SINK;
  1132. case V4L2_SEL_TGT_COMPOSE:
  1133. return pad ? R_INVALID : R_COMPOSE;
  1134. default:
  1135. return R_INVALID;
  1136. }
  1137. }
  1138. static int s5k5baf_is_bound_target(u32 target)
  1139. {
  1140. return target == V4L2_SEL_TGT_CROP_BOUNDS ||
  1141. target == V4L2_SEL_TGT_COMPOSE_BOUNDS;
  1142. }
  1143. static int s5k5baf_get_selection(struct v4l2_subdev *sd,
  1144. struct v4l2_subdev_pad_config *cfg,
  1145. struct v4l2_subdev_selection *sel)
  1146. {
  1147. static enum selection_rect rtype;
  1148. struct s5k5baf *state = to_s5k5baf(sd);
  1149. rtype = s5k5baf_get_sel_rect(sel->pad, sel->target);
  1150. switch (rtype) {
  1151. case R_INVALID:
  1152. return -EINVAL;
  1153. case R_CIS:
  1154. sel->r = s5k5baf_cis_rect;
  1155. return 0;
  1156. default:
  1157. break;
  1158. }
  1159. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  1160. if (rtype == R_COMPOSE)
  1161. sel->r = *v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
  1162. else
  1163. sel->r = *v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
  1164. return 0;
  1165. }
  1166. mutex_lock(&state->lock);
  1167. switch (rtype) {
  1168. case R_CROP_SINK:
  1169. sel->r = state->crop_sink;
  1170. break;
  1171. case R_COMPOSE:
  1172. sel->r = state->compose;
  1173. break;
  1174. case R_CROP_SOURCE:
  1175. sel->r = state->crop_source;
  1176. break;
  1177. default:
  1178. break;
  1179. }
  1180. if (s5k5baf_is_bound_target(sel->target)) {
  1181. sel->r.left = 0;
  1182. sel->r.top = 0;
  1183. }
  1184. mutex_unlock(&state->lock);
  1185. return 0;
  1186. }
  1187. /* bounds range [start, start+len) to [0, max) and aligns to 2 */
  1188. static void s5k5baf_bound_range(u32 *start, u32 *len, u32 max)
  1189. {
  1190. if (*len > max)
  1191. *len = max;
  1192. if (*start + *len > max)
  1193. *start = max - *len;
  1194. *start &= ~1;
  1195. *len &= ~1;
  1196. if (*len < S5K5BAF_WIN_WIDTH_MIN)
  1197. *len = S5K5BAF_WIN_WIDTH_MIN;
  1198. }
  1199. static void s5k5baf_bound_rect(struct v4l2_rect *r, u32 width, u32 height)
  1200. {
  1201. s5k5baf_bound_range(&r->left, &r->width, width);
  1202. s5k5baf_bound_range(&r->top, &r->height, height);
  1203. }
  1204. static void s5k5baf_set_rect_and_adjust(struct v4l2_rect **rects,
  1205. enum selection_rect first,
  1206. struct v4l2_rect *v)
  1207. {
  1208. struct v4l2_rect *r, *br;
  1209. enum selection_rect i = first;
  1210. *rects[first] = *v;
  1211. do {
  1212. r = rects[i];
  1213. br = rects[i - 1];
  1214. s5k5baf_bound_rect(r, br->width, br->height);
  1215. } while (++i != R_INVALID);
  1216. *v = *rects[first];
  1217. }
  1218. static bool s5k5baf_cmp_rect(const struct v4l2_rect *r1,
  1219. const struct v4l2_rect *r2)
  1220. {
  1221. return !memcmp(r1, r2, sizeof(*r1));
  1222. }
  1223. static int s5k5baf_set_selection(struct v4l2_subdev *sd,
  1224. struct v4l2_subdev_pad_config *cfg,
  1225. struct v4l2_subdev_selection *sel)
  1226. {
  1227. static enum selection_rect rtype;
  1228. struct s5k5baf *state = to_s5k5baf(sd);
  1229. struct v4l2_rect **rects;
  1230. int ret = 0;
  1231. rtype = s5k5baf_get_sel_rect(sel->pad, sel->target);
  1232. if (rtype == R_INVALID || s5k5baf_is_bound_target(sel->target))
  1233. return -EINVAL;
  1234. /* allow only scaling on compose */
  1235. if (rtype == R_COMPOSE) {
  1236. sel->r.left = 0;
  1237. sel->r.top = 0;
  1238. }
  1239. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  1240. rects = (struct v4l2_rect * []) {
  1241. &s5k5baf_cis_rect,
  1242. v4l2_subdev_get_try_crop(sd, cfg, PAD_CIS),
  1243. v4l2_subdev_get_try_compose(sd, cfg, PAD_CIS),
  1244. v4l2_subdev_get_try_crop(sd, cfg, PAD_OUT)
  1245. };
  1246. s5k5baf_set_rect_and_adjust(rects, rtype, &sel->r);
  1247. return 0;
  1248. }
  1249. rects = (struct v4l2_rect * []) {
  1250. &s5k5baf_cis_rect,
  1251. &state->crop_sink,
  1252. &state->compose,
  1253. &state->crop_source
  1254. };
  1255. mutex_lock(&state->lock);
  1256. if (state->streaming) {
  1257. /* adjust sel->r to avoid output resolution change */
  1258. if (rtype < R_CROP_SOURCE) {
  1259. if (sel->r.width < state->crop_source.width)
  1260. sel->r.width = state->crop_source.width;
  1261. if (sel->r.height < state->crop_source.height)
  1262. sel->r.height = state->crop_source.height;
  1263. } else {
  1264. sel->r.width = state->crop_source.width;
  1265. sel->r.height = state->crop_source.height;
  1266. }
  1267. }
  1268. s5k5baf_set_rect_and_adjust(rects, rtype, &sel->r);
  1269. if (!s5k5baf_cmp_rect(&state->crop_sink, &s5k5baf_cis_rect) ||
  1270. !s5k5baf_cmp_rect(&state->compose, &s5k5baf_cis_rect))
  1271. state->apply_crop = 1;
  1272. if (state->streaming)
  1273. ret = s5k5baf_hw_set_crop_rects(state);
  1274. mutex_unlock(&state->lock);
  1275. return ret;
  1276. }
  1277. static const struct v4l2_subdev_pad_ops s5k5baf_cis_pad_ops = {
  1278. .enum_mbus_code = s5k5baf_enum_mbus_code,
  1279. .enum_frame_size = s5k5baf_enum_frame_size,
  1280. .get_fmt = s5k5baf_get_fmt,
  1281. .set_fmt = s5k5baf_set_fmt,
  1282. };
  1283. static const struct v4l2_subdev_pad_ops s5k5baf_pad_ops = {
  1284. .enum_mbus_code = s5k5baf_enum_mbus_code,
  1285. .enum_frame_size = s5k5baf_enum_frame_size,
  1286. .enum_frame_interval = s5k5baf_enum_frame_interval,
  1287. .get_fmt = s5k5baf_get_fmt,
  1288. .set_fmt = s5k5baf_set_fmt,
  1289. .get_selection = s5k5baf_get_selection,
  1290. .set_selection = s5k5baf_set_selection,
  1291. };
  1292. static const struct v4l2_subdev_video_ops s5k5baf_video_ops = {
  1293. .g_frame_interval = s5k5baf_g_frame_interval,
  1294. .s_frame_interval = s5k5baf_s_frame_interval,
  1295. .s_stream = s5k5baf_s_stream,
  1296. };
  1297. /*
  1298. * V4L2 subdev controls
  1299. */
  1300. static int s5k5baf_s_ctrl(struct v4l2_ctrl *ctrl)
  1301. {
  1302. struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
  1303. struct s5k5baf *state = to_s5k5baf(sd);
  1304. int ret;
  1305. v4l2_dbg(1, debug, sd, "ctrl: %s, value: %d\n", ctrl->name, ctrl->val);
  1306. mutex_lock(&state->lock);
  1307. if (state->power == 0)
  1308. goto unlock;
  1309. switch (ctrl->id) {
  1310. case V4L2_CID_AUTO_WHITE_BALANCE:
  1311. s5k5baf_hw_set_awb(state, ctrl->val);
  1312. break;
  1313. case V4L2_CID_BRIGHTNESS:
  1314. s5k5baf_write(state, REG_USER_BRIGHTNESS, ctrl->val);
  1315. break;
  1316. case V4L2_CID_COLORFX:
  1317. s5k5baf_hw_set_colorfx(state, ctrl->val);
  1318. break;
  1319. case V4L2_CID_CONTRAST:
  1320. s5k5baf_write(state, REG_USER_CONTRAST, ctrl->val);
  1321. break;
  1322. case V4L2_CID_EXPOSURE_AUTO:
  1323. s5k5baf_hw_set_auto_exposure(state, ctrl->val);
  1324. break;
  1325. case V4L2_CID_HFLIP:
  1326. s5k5baf_hw_set_mirror(state);
  1327. break;
  1328. case V4L2_CID_POWER_LINE_FREQUENCY:
  1329. s5k5baf_hw_set_anti_flicker(state, ctrl->val);
  1330. break;
  1331. case V4L2_CID_SATURATION:
  1332. s5k5baf_write(state, REG_USER_SATURATION, ctrl->val);
  1333. break;
  1334. case V4L2_CID_SHARPNESS:
  1335. s5k5baf_write(state, REG_USER_SHARPBLUR, ctrl->val);
  1336. break;
  1337. case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
  1338. s5k5baf_write(state, REG_P_COLORTEMP(0), ctrl->val);
  1339. if (state->apply_cfg)
  1340. s5k5baf_hw_sync_cfg(state);
  1341. break;
  1342. case V4L2_CID_TEST_PATTERN:
  1343. s5k5baf_hw_set_test_pattern(state, ctrl->val);
  1344. break;
  1345. }
  1346. unlock:
  1347. ret = s5k5baf_clear_error(state);
  1348. mutex_unlock(&state->lock);
  1349. return ret;
  1350. }
  1351. static const struct v4l2_ctrl_ops s5k5baf_ctrl_ops = {
  1352. .s_ctrl = s5k5baf_s_ctrl,
  1353. };
  1354. static const char * const s5k5baf_test_pattern_menu[] = {
  1355. "Disabled",
  1356. "Blank",
  1357. "Bars",
  1358. "Gradients",
  1359. "Textile",
  1360. "Textile2",
  1361. "Squares"
  1362. };
  1363. static int s5k5baf_initialize_ctrls(struct s5k5baf *state)
  1364. {
  1365. const struct v4l2_ctrl_ops *ops = &s5k5baf_ctrl_ops;
  1366. struct s5k5baf_ctrls *ctrls = &state->ctrls;
  1367. struct v4l2_ctrl_handler *hdl = &ctrls->handler;
  1368. int ret;
  1369. ret = v4l2_ctrl_handler_init(hdl, 16);
  1370. if (ret < 0) {
  1371. v4l2_err(&state->sd, "cannot init ctrl handler (%d)\n", ret);
  1372. return ret;
  1373. }
  1374. /* Auto white balance cluster */
  1375. ctrls->awb = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTO_WHITE_BALANCE,
  1376. 0, 1, 1, 1);
  1377. ctrls->gain_red = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
  1378. 0, 255, 1, S5K5BAF_GAIN_RED_DEF);
  1379. ctrls->gain_blue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
  1380. 0, 255, 1, S5K5BAF_GAIN_BLUE_DEF);
  1381. v4l2_ctrl_auto_cluster(3, &ctrls->awb, 0, false);
  1382. ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  1383. ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  1384. v4l2_ctrl_cluster(2, &ctrls->hflip);
  1385. ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
  1386. V4L2_CID_EXPOSURE_AUTO,
  1387. V4L2_EXPOSURE_MANUAL, 0, V4L2_EXPOSURE_AUTO);
  1388. /* Exposure time: x 1 us */
  1389. ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
  1390. 0, 6000000U, 1, 100000U);
  1391. /* Total gain: 256 <=> 1x */
  1392. ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
  1393. 0, 256, 1, 256);
  1394. v4l2_ctrl_auto_cluster(3, &ctrls->auto_exp, 0, false);
  1395. v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_POWER_LINE_FREQUENCY,
  1396. V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
  1397. V4L2_CID_POWER_LINE_FREQUENCY_AUTO);
  1398. v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_COLORFX,
  1399. V4L2_COLORFX_SKY_BLUE, ~0x6f, V4L2_COLORFX_NONE);
  1400. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_WHITE_BALANCE_TEMPERATURE,
  1401. 0, 256, 1, 0);
  1402. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, -127, 127, 1, 0);
  1403. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS, -127, 127, 1, 0);
  1404. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, -127, 127, 1, 0);
  1405. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SHARPNESS, -127, 127, 1, 0);
  1406. v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
  1407. ARRAY_SIZE(s5k5baf_test_pattern_menu) - 1,
  1408. 0, 0, s5k5baf_test_pattern_menu);
  1409. if (hdl->error) {
  1410. v4l2_err(&state->sd, "error creating controls (%d)\n",
  1411. hdl->error);
  1412. ret = hdl->error;
  1413. v4l2_ctrl_handler_free(hdl);
  1414. return ret;
  1415. }
  1416. state->sd.ctrl_handler = hdl;
  1417. return 0;
  1418. }
  1419. /*
  1420. * V4L2 subdev internal operations
  1421. */
  1422. static int s5k5baf_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  1423. {
  1424. struct v4l2_mbus_framefmt *mf;
  1425. mf = v4l2_subdev_get_try_format(sd, fh->pad, PAD_CIS);
  1426. s5k5baf_try_cis_format(mf);
  1427. if (s5k5baf_is_cis_subdev(sd))
  1428. return 0;
  1429. mf = v4l2_subdev_get_try_format(sd, fh->pad, PAD_OUT);
  1430. mf->colorspace = s5k5baf_formats[0].colorspace;
  1431. mf->code = s5k5baf_formats[0].code;
  1432. mf->width = s5k5baf_cis_rect.width;
  1433. mf->height = s5k5baf_cis_rect.height;
  1434. mf->field = V4L2_FIELD_NONE;
  1435. *v4l2_subdev_get_try_crop(sd, fh->pad, PAD_CIS) = s5k5baf_cis_rect;
  1436. *v4l2_subdev_get_try_compose(sd, fh->pad, PAD_CIS) = s5k5baf_cis_rect;
  1437. *v4l2_subdev_get_try_crop(sd, fh->pad, PAD_OUT) = s5k5baf_cis_rect;
  1438. return 0;
  1439. }
  1440. static int s5k5baf_check_fw_revision(struct s5k5baf *state)
  1441. {
  1442. u16 api_ver = 0, fw_rev = 0, s_id = 0;
  1443. int ret;
  1444. api_ver = s5k5baf_read(state, REG_FW_APIVER);
  1445. fw_rev = s5k5baf_read(state, REG_FW_REVISION) & 0xff;
  1446. s_id = s5k5baf_read(state, REG_FW_SENSOR_ID);
  1447. ret = s5k5baf_clear_error(state);
  1448. if (ret < 0)
  1449. return ret;
  1450. v4l2_info(&state->sd, "FW API=%#x, revision=%#x sensor_id=%#x\n",
  1451. api_ver, fw_rev, s_id);
  1452. if (api_ver != S5K5BAF_FW_APIVER) {
  1453. v4l2_err(&state->sd, "FW API version not supported\n");
  1454. return -ENODEV;
  1455. }
  1456. return 0;
  1457. }
  1458. static int s5k5baf_registered(struct v4l2_subdev *sd)
  1459. {
  1460. struct s5k5baf *state = to_s5k5baf(sd);
  1461. int ret;
  1462. ret = v4l2_device_register_subdev(sd->v4l2_dev, &state->cis_sd);
  1463. if (ret < 0)
  1464. v4l2_err(sd, "failed to register subdev %s\n",
  1465. state->cis_sd.name);
  1466. else
  1467. ret = media_entity_create_link(&state->cis_sd.entity, PAD_CIS,
  1468. &state->sd.entity, PAD_CIS,
  1469. MEDIA_LNK_FL_IMMUTABLE |
  1470. MEDIA_LNK_FL_ENABLED);
  1471. return ret;
  1472. }
  1473. static void s5k5baf_unregistered(struct v4l2_subdev *sd)
  1474. {
  1475. struct s5k5baf *state = to_s5k5baf(sd);
  1476. v4l2_device_unregister_subdev(&state->cis_sd);
  1477. }
  1478. static const struct v4l2_subdev_ops s5k5baf_cis_subdev_ops = {
  1479. .pad = &s5k5baf_cis_pad_ops,
  1480. };
  1481. static const struct v4l2_subdev_internal_ops s5k5baf_cis_subdev_internal_ops = {
  1482. .open = s5k5baf_open,
  1483. };
  1484. static const struct v4l2_subdev_internal_ops s5k5baf_subdev_internal_ops = {
  1485. .registered = s5k5baf_registered,
  1486. .unregistered = s5k5baf_unregistered,
  1487. .open = s5k5baf_open,
  1488. };
  1489. static const struct v4l2_subdev_core_ops s5k5baf_core_ops = {
  1490. .s_power = s5k5baf_set_power,
  1491. .log_status = v4l2_ctrl_subdev_log_status,
  1492. };
  1493. static const struct v4l2_subdev_ops s5k5baf_subdev_ops = {
  1494. .core = &s5k5baf_core_ops,
  1495. .pad = &s5k5baf_pad_ops,
  1496. .video = &s5k5baf_video_ops,
  1497. };
  1498. static int s5k5baf_configure_gpios(struct s5k5baf *state)
  1499. {
  1500. static const char * const name[] = { "S5K5BAF_STBY", "S5K5BAF_RST" };
  1501. struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
  1502. struct s5k5baf_gpio *g = state->gpios;
  1503. int ret, i;
  1504. for (i = 0; i < NUM_GPIOS; ++i) {
  1505. int flags = GPIOF_DIR_OUT;
  1506. if (g[i].level)
  1507. flags |= GPIOF_INIT_HIGH;
  1508. ret = devm_gpio_request_one(&c->dev, g[i].gpio, flags, name[i]);
  1509. if (ret < 0) {
  1510. v4l2_err(c, "failed to request gpio %s\n", name[i]);
  1511. return ret;
  1512. }
  1513. }
  1514. return 0;
  1515. }
  1516. static int s5k5baf_parse_gpios(struct s5k5baf_gpio *gpios, struct device *dev)
  1517. {
  1518. static const char * const names[] = {
  1519. "stbyn-gpios",
  1520. "rstn-gpios",
  1521. };
  1522. struct device_node *node = dev->of_node;
  1523. enum of_gpio_flags flags;
  1524. int ret, i;
  1525. for (i = 0; i < NUM_GPIOS; ++i) {
  1526. ret = of_get_named_gpio_flags(node, names[i], 0, &flags);
  1527. if (ret < 0) {
  1528. dev_err(dev, "no %s GPIO pin provided\n", names[i]);
  1529. return ret;
  1530. }
  1531. gpios[i].gpio = ret;
  1532. gpios[i].level = !(flags & OF_GPIO_ACTIVE_LOW);
  1533. }
  1534. return 0;
  1535. }
  1536. static int s5k5baf_parse_device_node(struct s5k5baf *state, struct device *dev)
  1537. {
  1538. struct device_node *node = dev->of_node;
  1539. struct device_node *node_ep;
  1540. struct v4l2_of_endpoint ep;
  1541. int ret;
  1542. if (!node) {
  1543. dev_err(dev, "no device-tree node provided\n");
  1544. return -EINVAL;
  1545. }
  1546. ret = of_property_read_u32(node, "clock-frequency",
  1547. &state->mclk_frequency);
  1548. if (ret < 0) {
  1549. state->mclk_frequency = S5K5BAF_DEFAULT_MCLK_FREQ;
  1550. dev_info(dev, "using default %u Hz clock frequency\n",
  1551. state->mclk_frequency);
  1552. }
  1553. ret = s5k5baf_parse_gpios(state->gpios, dev);
  1554. if (ret < 0)
  1555. return ret;
  1556. node_ep = of_graph_get_next_endpoint(node, NULL);
  1557. if (!node_ep) {
  1558. dev_err(dev, "no endpoint defined at node %s\n",
  1559. node->full_name);
  1560. return -EINVAL;
  1561. }
  1562. v4l2_of_parse_endpoint(node_ep, &ep);
  1563. of_node_put(node_ep);
  1564. state->bus_type = ep.bus_type;
  1565. switch (state->bus_type) {
  1566. case V4L2_MBUS_CSI2:
  1567. state->nlanes = ep.bus.mipi_csi2.num_data_lanes;
  1568. break;
  1569. case V4L2_MBUS_PARALLEL:
  1570. break;
  1571. default:
  1572. dev_err(dev, "unsupported bus in endpoint defined at node %s\n",
  1573. node->full_name);
  1574. return -EINVAL;
  1575. }
  1576. return 0;
  1577. }
  1578. static int s5k5baf_configure_subdevs(struct s5k5baf *state,
  1579. struct i2c_client *c)
  1580. {
  1581. struct v4l2_subdev *sd;
  1582. int ret;
  1583. sd = &state->cis_sd;
  1584. v4l2_subdev_init(sd, &s5k5baf_cis_subdev_ops);
  1585. sd->owner = THIS_MODULE;
  1586. v4l2_set_subdevdata(sd, state);
  1587. snprintf(sd->name, sizeof(sd->name), "S5K5BAF-CIS %d-%04x",
  1588. i2c_adapter_id(c->adapter), c->addr);
  1589. sd->internal_ops = &s5k5baf_cis_subdev_internal_ops;
  1590. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1591. state->cis_pad.flags = MEDIA_PAD_FL_SOURCE;
  1592. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
  1593. ret = media_entity_init(&sd->entity, NUM_CIS_PADS, &state->cis_pad, 0);
  1594. if (ret < 0)
  1595. goto err;
  1596. sd = &state->sd;
  1597. v4l2_i2c_subdev_init(sd, c, &s5k5baf_subdev_ops);
  1598. snprintf(sd->name, sizeof(sd->name), "S5K5BAF-ISP %d-%04x",
  1599. i2c_adapter_id(c->adapter), c->addr);
  1600. sd->internal_ops = &s5k5baf_subdev_internal_ops;
  1601. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1602. state->pads[PAD_CIS].flags = MEDIA_PAD_FL_SINK;
  1603. state->pads[PAD_OUT].flags = MEDIA_PAD_FL_SOURCE;
  1604. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
  1605. ret = media_entity_init(&sd->entity, NUM_ISP_PADS, state->pads, 0);
  1606. if (!ret)
  1607. return 0;
  1608. media_entity_cleanup(&state->cis_sd.entity);
  1609. err:
  1610. dev_err(&c->dev, "cannot init media entity %s\n", sd->name);
  1611. return ret;
  1612. }
  1613. static int s5k5baf_configure_regulators(struct s5k5baf *state)
  1614. {
  1615. struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
  1616. int ret;
  1617. int i;
  1618. for (i = 0; i < S5K5BAF_NUM_SUPPLIES; i++)
  1619. state->supplies[i].supply = s5k5baf_supply_names[i];
  1620. ret = devm_regulator_bulk_get(&c->dev, S5K5BAF_NUM_SUPPLIES,
  1621. state->supplies);
  1622. if (ret < 0)
  1623. v4l2_err(c, "failed to get regulators\n");
  1624. return ret;
  1625. }
  1626. static int s5k5baf_probe(struct i2c_client *c,
  1627. const struct i2c_device_id *id)
  1628. {
  1629. struct s5k5baf *state;
  1630. int ret;
  1631. state = devm_kzalloc(&c->dev, sizeof(*state), GFP_KERNEL);
  1632. if (!state)
  1633. return -ENOMEM;
  1634. mutex_init(&state->lock);
  1635. state->crop_sink = s5k5baf_cis_rect;
  1636. state->compose = s5k5baf_cis_rect;
  1637. state->crop_source = s5k5baf_cis_rect;
  1638. ret = s5k5baf_parse_device_node(state, &c->dev);
  1639. if (ret < 0)
  1640. return ret;
  1641. ret = s5k5baf_configure_subdevs(state, c);
  1642. if (ret < 0)
  1643. return ret;
  1644. ret = s5k5baf_configure_gpios(state);
  1645. if (ret < 0)
  1646. goto err_me;
  1647. ret = s5k5baf_configure_regulators(state);
  1648. if (ret < 0)
  1649. goto err_me;
  1650. state->clock = devm_clk_get(state->sd.dev, S5K5BAF_CLK_NAME);
  1651. if (IS_ERR(state->clock)) {
  1652. ret = -EPROBE_DEFER;
  1653. goto err_me;
  1654. }
  1655. ret = s5k5baf_power_on(state);
  1656. if (ret < 0) {
  1657. ret = -EPROBE_DEFER;
  1658. goto err_me;
  1659. }
  1660. s5k5baf_hw_init(state);
  1661. ret = s5k5baf_check_fw_revision(state);
  1662. s5k5baf_power_off(state);
  1663. if (ret < 0)
  1664. goto err_me;
  1665. ret = s5k5baf_initialize_ctrls(state);
  1666. if (ret < 0)
  1667. goto err_me;
  1668. ret = v4l2_async_register_subdev(&state->sd);
  1669. if (ret < 0)
  1670. goto err_ctrl;
  1671. return 0;
  1672. err_ctrl:
  1673. v4l2_ctrl_handler_free(state->sd.ctrl_handler);
  1674. err_me:
  1675. media_entity_cleanup(&state->sd.entity);
  1676. media_entity_cleanup(&state->cis_sd.entity);
  1677. return ret;
  1678. }
  1679. static int s5k5baf_remove(struct i2c_client *c)
  1680. {
  1681. struct v4l2_subdev *sd = i2c_get_clientdata(c);
  1682. struct s5k5baf *state = to_s5k5baf(sd);
  1683. v4l2_async_unregister_subdev(sd);
  1684. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1685. media_entity_cleanup(&sd->entity);
  1686. sd = &state->cis_sd;
  1687. v4l2_device_unregister_subdev(sd);
  1688. media_entity_cleanup(&sd->entity);
  1689. return 0;
  1690. }
  1691. static const struct i2c_device_id s5k5baf_id[] = {
  1692. { S5K5BAF_DRIVER_NAME, 0 },
  1693. { },
  1694. };
  1695. MODULE_DEVICE_TABLE(i2c, s5k5baf_id);
  1696. static const struct of_device_id s5k5baf_of_match[] = {
  1697. { .compatible = "samsung,s5k5baf" },
  1698. { }
  1699. };
  1700. MODULE_DEVICE_TABLE(of, s5k5baf_of_match);
  1701. static struct i2c_driver s5k5baf_i2c_driver = {
  1702. .driver = {
  1703. .of_match_table = s5k5baf_of_match,
  1704. .name = S5K5BAF_DRIVER_NAME
  1705. },
  1706. .probe = s5k5baf_probe,
  1707. .remove = s5k5baf_remove,
  1708. .id_table = s5k5baf_id,
  1709. };
  1710. module_i2c_driver(s5k5baf_i2c_driver);
  1711. MODULE_DESCRIPTION("Samsung S5K5BAF(X) UXGA camera driver");
  1712. MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
  1713. MODULE_LICENSE("GPL v2");