s5k5baf.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056
  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 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 = MEDIA_BUS_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 = MEDIA_BUS_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 = MEDIA_BUS_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. mf->field = V4L2_FIELD_NONE;
  1097. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1098. *v4l2_subdev_get_try_format(fh, fmt->pad) = *mf;
  1099. return 0;
  1100. }
  1101. if (fmt->pad == PAD_CIS) {
  1102. s5k5baf_try_cis_format(mf);
  1103. return 0;
  1104. }
  1105. mutex_lock(&state->lock);
  1106. if (state->streaming) {
  1107. mutex_unlock(&state->lock);
  1108. return -EBUSY;
  1109. }
  1110. state->pixfmt = s5k5baf_try_isp_format(mf);
  1111. pixfmt = &s5k5baf_formats[state->pixfmt];
  1112. mf->code = pixfmt->code;
  1113. mf->colorspace = pixfmt->colorspace;
  1114. mf->width = state->crop_source.width;
  1115. mf->height = state->crop_source.height;
  1116. mutex_unlock(&state->lock);
  1117. return ret;
  1118. }
  1119. enum selection_rect { R_CIS, R_CROP_SINK, R_COMPOSE, R_CROP_SOURCE, R_INVALID };
  1120. static enum selection_rect s5k5baf_get_sel_rect(u32 pad, u32 target)
  1121. {
  1122. switch (target) {
  1123. case V4L2_SEL_TGT_CROP_BOUNDS:
  1124. return pad ? R_COMPOSE : R_CIS;
  1125. case V4L2_SEL_TGT_CROP:
  1126. return pad ? R_CROP_SOURCE : R_CROP_SINK;
  1127. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  1128. return pad ? R_INVALID : R_CROP_SINK;
  1129. case V4L2_SEL_TGT_COMPOSE:
  1130. return pad ? R_INVALID : R_COMPOSE;
  1131. default:
  1132. return R_INVALID;
  1133. }
  1134. }
  1135. static int s5k5baf_is_bound_target(u32 target)
  1136. {
  1137. return target == V4L2_SEL_TGT_CROP_BOUNDS ||
  1138. target == V4L2_SEL_TGT_COMPOSE_BOUNDS;
  1139. }
  1140. static int s5k5baf_get_selection(struct v4l2_subdev *sd,
  1141. struct v4l2_subdev_fh *fh,
  1142. struct v4l2_subdev_selection *sel)
  1143. {
  1144. static enum selection_rect rtype;
  1145. struct s5k5baf *state = to_s5k5baf(sd);
  1146. rtype = s5k5baf_get_sel_rect(sel->pad, sel->target);
  1147. switch (rtype) {
  1148. case R_INVALID:
  1149. return -EINVAL;
  1150. case R_CIS:
  1151. sel->r = s5k5baf_cis_rect;
  1152. return 0;
  1153. default:
  1154. break;
  1155. }
  1156. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  1157. if (rtype == R_COMPOSE)
  1158. sel->r = *v4l2_subdev_get_try_compose(fh, sel->pad);
  1159. else
  1160. sel->r = *v4l2_subdev_get_try_crop(fh, sel->pad);
  1161. return 0;
  1162. }
  1163. mutex_lock(&state->lock);
  1164. switch (rtype) {
  1165. case R_CROP_SINK:
  1166. sel->r = state->crop_sink;
  1167. break;
  1168. case R_COMPOSE:
  1169. sel->r = state->compose;
  1170. break;
  1171. case R_CROP_SOURCE:
  1172. sel->r = state->crop_source;
  1173. break;
  1174. default:
  1175. break;
  1176. }
  1177. if (s5k5baf_is_bound_target(sel->target)) {
  1178. sel->r.left = 0;
  1179. sel->r.top = 0;
  1180. }
  1181. mutex_unlock(&state->lock);
  1182. return 0;
  1183. }
  1184. /* bounds range [start, start+len) to [0, max) and aligns to 2 */
  1185. static void s5k5baf_bound_range(u32 *start, u32 *len, u32 max)
  1186. {
  1187. if (*len > max)
  1188. *len = max;
  1189. if (*start + *len > max)
  1190. *start = max - *len;
  1191. *start &= ~1;
  1192. *len &= ~1;
  1193. if (*len < S5K5BAF_WIN_WIDTH_MIN)
  1194. *len = S5K5BAF_WIN_WIDTH_MIN;
  1195. }
  1196. static void s5k5baf_bound_rect(struct v4l2_rect *r, u32 width, u32 height)
  1197. {
  1198. s5k5baf_bound_range(&r->left, &r->width, width);
  1199. s5k5baf_bound_range(&r->top, &r->height, height);
  1200. }
  1201. static void s5k5baf_set_rect_and_adjust(struct v4l2_rect **rects,
  1202. enum selection_rect first,
  1203. struct v4l2_rect *v)
  1204. {
  1205. struct v4l2_rect *r, *br;
  1206. enum selection_rect i = first;
  1207. *rects[first] = *v;
  1208. do {
  1209. r = rects[i];
  1210. br = rects[i - 1];
  1211. s5k5baf_bound_rect(r, br->width, br->height);
  1212. } while (++i != R_INVALID);
  1213. *v = *rects[first];
  1214. }
  1215. static bool s5k5baf_cmp_rect(const struct v4l2_rect *r1,
  1216. const struct v4l2_rect *r2)
  1217. {
  1218. return !memcmp(r1, r2, sizeof(*r1));
  1219. }
  1220. static int s5k5baf_set_selection(struct v4l2_subdev *sd,
  1221. struct v4l2_subdev_fh *fh,
  1222. struct v4l2_subdev_selection *sel)
  1223. {
  1224. static enum selection_rect rtype;
  1225. struct s5k5baf *state = to_s5k5baf(sd);
  1226. struct v4l2_rect **rects;
  1227. int ret = 0;
  1228. rtype = s5k5baf_get_sel_rect(sel->pad, sel->target);
  1229. if (rtype == R_INVALID || s5k5baf_is_bound_target(sel->target))
  1230. return -EINVAL;
  1231. /* allow only scaling on compose */
  1232. if (rtype == R_COMPOSE) {
  1233. sel->r.left = 0;
  1234. sel->r.top = 0;
  1235. }
  1236. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  1237. rects = (struct v4l2_rect * []) {
  1238. &s5k5baf_cis_rect,
  1239. v4l2_subdev_get_try_crop(fh, PAD_CIS),
  1240. v4l2_subdev_get_try_compose(fh, PAD_CIS),
  1241. v4l2_subdev_get_try_crop(fh, PAD_OUT)
  1242. };
  1243. s5k5baf_set_rect_and_adjust(rects, rtype, &sel->r);
  1244. return 0;
  1245. }
  1246. rects = (struct v4l2_rect * []) {
  1247. &s5k5baf_cis_rect,
  1248. &state->crop_sink,
  1249. &state->compose,
  1250. &state->crop_source
  1251. };
  1252. mutex_lock(&state->lock);
  1253. if (state->streaming) {
  1254. /* adjust sel->r to avoid output resolution change */
  1255. if (rtype < R_CROP_SOURCE) {
  1256. if (sel->r.width < state->crop_source.width)
  1257. sel->r.width = state->crop_source.width;
  1258. if (sel->r.height < state->crop_source.height)
  1259. sel->r.height = state->crop_source.height;
  1260. } else {
  1261. sel->r.width = state->crop_source.width;
  1262. sel->r.height = state->crop_source.height;
  1263. }
  1264. }
  1265. s5k5baf_set_rect_and_adjust(rects, rtype, &sel->r);
  1266. if (!s5k5baf_cmp_rect(&state->crop_sink, &s5k5baf_cis_rect) ||
  1267. !s5k5baf_cmp_rect(&state->compose, &s5k5baf_cis_rect))
  1268. state->apply_crop = 1;
  1269. if (state->streaming)
  1270. ret = s5k5baf_hw_set_crop_rects(state);
  1271. mutex_unlock(&state->lock);
  1272. return ret;
  1273. }
  1274. static const struct v4l2_subdev_pad_ops s5k5baf_cis_pad_ops = {
  1275. .enum_mbus_code = s5k5baf_enum_mbus_code,
  1276. .enum_frame_size = s5k5baf_enum_frame_size,
  1277. .get_fmt = s5k5baf_get_fmt,
  1278. .set_fmt = s5k5baf_set_fmt,
  1279. };
  1280. static const struct v4l2_subdev_pad_ops s5k5baf_pad_ops = {
  1281. .enum_mbus_code = s5k5baf_enum_mbus_code,
  1282. .enum_frame_size = s5k5baf_enum_frame_size,
  1283. .enum_frame_interval = s5k5baf_enum_frame_interval,
  1284. .get_fmt = s5k5baf_get_fmt,
  1285. .set_fmt = s5k5baf_set_fmt,
  1286. .get_selection = s5k5baf_get_selection,
  1287. .set_selection = s5k5baf_set_selection,
  1288. };
  1289. static const struct v4l2_subdev_video_ops s5k5baf_video_ops = {
  1290. .g_frame_interval = s5k5baf_g_frame_interval,
  1291. .s_frame_interval = s5k5baf_s_frame_interval,
  1292. .s_stream = s5k5baf_s_stream,
  1293. };
  1294. /*
  1295. * V4L2 subdev controls
  1296. */
  1297. static int s5k5baf_s_ctrl(struct v4l2_ctrl *ctrl)
  1298. {
  1299. struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
  1300. struct s5k5baf *state = to_s5k5baf(sd);
  1301. int ret;
  1302. v4l2_dbg(1, debug, sd, "ctrl: %s, value: %d\n", ctrl->name, ctrl->val);
  1303. mutex_lock(&state->lock);
  1304. if (state->power == 0)
  1305. goto unlock;
  1306. switch (ctrl->id) {
  1307. case V4L2_CID_AUTO_WHITE_BALANCE:
  1308. s5k5baf_hw_set_awb(state, ctrl->val);
  1309. break;
  1310. case V4L2_CID_BRIGHTNESS:
  1311. s5k5baf_write(state, REG_USER_BRIGHTNESS, ctrl->val);
  1312. break;
  1313. case V4L2_CID_COLORFX:
  1314. s5k5baf_hw_set_colorfx(state, ctrl->val);
  1315. break;
  1316. case V4L2_CID_CONTRAST:
  1317. s5k5baf_write(state, REG_USER_CONTRAST, ctrl->val);
  1318. break;
  1319. case V4L2_CID_EXPOSURE_AUTO:
  1320. s5k5baf_hw_set_auto_exposure(state, ctrl->val);
  1321. break;
  1322. case V4L2_CID_HFLIP:
  1323. s5k5baf_hw_set_mirror(state);
  1324. break;
  1325. case V4L2_CID_POWER_LINE_FREQUENCY:
  1326. s5k5baf_hw_set_anti_flicker(state, ctrl->val);
  1327. break;
  1328. case V4L2_CID_SATURATION:
  1329. s5k5baf_write(state, REG_USER_SATURATION, ctrl->val);
  1330. break;
  1331. case V4L2_CID_SHARPNESS:
  1332. s5k5baf_write(state, REG_USER_SHARPBLUR, ctrl->val);
  1333. break;
  1334. case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
  1335. s5k5baf_write(state, REG_P_COLORTEMP(0), ctrl->val);
  1336. if (state->apply_cfg)
  1337. s5k5baf_hw_sync_cfg(state);
  1338. break;
  1339. case V4L2_CID_TEST_PATTERN:
  1340. s5k5baf_hw_set_test_pattern(state, ctrl->val);
  1341. break;
  1342. }
  1343. unlock:
  1344. ret = s5k5baf_clear_error(state);
  1345. mutex_unlock(&state->lock);
  1346. return ret;
  1347. }
  1348. static const struct v4l2_ctrl_ops s5k5baf_ctrl_ops = {
  1349. .s_ctrl = s5k5baf_s_ctrl,
  1350. };
  1351. static const char * const s5k5baf_test_pattern_menu[] = {
  1352. "Disabled",
  1353. "Blank",
  1354. "Bars",
  1355. "Gradients",
  1356. "Textile",
  1357. "Textile2",
  1358. "Squares"
  1359. };
  1360. static int s5k5baf_initialize_ctrls(struct s5k5baf *state)
  1361. {
  1362. const struct v4l2_ctrl_ops *ops = &s5k5baf_ctrl_ops;
  1363. struct s5k5baf_ctrls *ctrls = &state->ctrls;
  1364. struct v4l2_ctrl_handler *hdl = &ctrls->handler;
  1365. int ret;
  1366. ret = v4l2_ctrl_handler_init(hdl, 16);
  1367. if (ret < 0) {
  1368. v4l2_err(&state->sd, "cannot init ctrl handler (%d)\n", ret);
  1369. return ret;
  1370. }
  1371. /* Auto white balance cluster */
  1372. ctrls->awb = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTO_WHITE_BALANCE,
  1373. 0, 1, 1, 1);
  1374. ctrls->gain_red = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
  1375. 0, 255, 1, S5K5BAF_GAIN_RED_DEF);
  1376. ctrls->gain_blue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
  1377. 0, 255, 1, S5K5BAF_GAIN_BLUE_DEF);
  1378. v4l2_ctrl_auto_cluster(3, &ctrls->awb, 0, false);
  1379. ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  1380. ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  1381. v4l2_ctrl_cluster(2, &ctrls->hflip);
  1382. ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
  1383. V4L2_CID_EXPOSURE_AUTO,
  1384. V4L2_EXPOSURE_MANUAL, 0, V4L2_EXPOSURE_AUTO);
  1385. /* Exposure time: x 1 us */
  1386. ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
  1387. 0, 6000000U, 1, 100000U);
  1388. /* Total gain: 256 <=> 1x */
  1389. ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
  1390. 0, 256, 1, 256);
  1391. v4l2_ctrl_auto_cluster(3, &ctrls->auto_exp, 0, false);
  1392. v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_POWER_LINE_FREQUENCY,
  1393. V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
  1394. V4L2_CID_POWER_LINE_FREQUENCY_AUTO);
  1395. v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_COLORFX,
  1396. V4L2_COLORFX_SKY_BLUE, ~0x6f, V4L2_COLORFX_NONE);
  1397. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_WHITE_BALANCE_TEMPERATURE,
  1398. 0, 256, 1, 0);
  1399. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, -127, 127, 1, 0);
  1400. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS, -127, 127, 1, 0);
  1401. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, -127, 127, 1, 0);
  1402. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SHARPNESS, -127, 127, 1, 0);
  1403. v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
  1404. ARRAY_SIZE(s5k5baf_test_pattern_menu) - 1,
  1405. 0, 0, s5k5baf_test_pattern_menu);
  1406. if (hdl->error) {
  1407. v4l2_err(&state->sd, "error creating controls (%d)\n",
  1408. hdl->error);
  1409. ret = hdl->error;
  1410. v4l2_ctrl_handler_free(hdl);
  1411. return ret;
  1412. }
  1413. state->sd.ctrl_handler = hdl;
  1414. return 0;
  1415. }
  1416. /*
  1417. * V4L2 subdev internal operations
  1418. */
  1419. static int s5k5baf_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  1420. {
  1421. struct v4l2_mbus_framefmt *mf;
  1422. mf = v4l2_subdev_get_try_format(fh, PAD_CIS);
  1423. s5k5baf_try_cis_format(mf);
  1424. if (s5k5baf_is_cis_subdev(sd))
  1425. return 0;
  1426. mf = v4l2_subdev_get_try_format(fh, PAD_OUT);
  1427. mf->colorspace = s5k5baf_formats[0].colorspace;
  1428. mf->code = s5k5baf_formats[0].code;
  1429. mf->width = s5k5baf_cis_rect.width;
  1430. mf->height = s5k5baf_cis_rect.height;
  1431. mf->field = V4L2_FIELD_NONE;
  1432. *v4l2_subdev_get_try_crop(fh, PAD_CIS) = s5k5baf_cis_rect;
  1433. *v4l2_subdev_get_try_compose(fh, PAD_CIS) = s5k5baf_cis_rect;
  1434. *v4l2_subdev_get_try_crop(fh, PAD_OUT) = s5k5baf_cis_rect;
  1435. return 0;
  1436. }
  1437. static int s5k5baf_check_fw_revision(struct s5k5baf *state)
  1438. {
  1439. u16 api_ver = 0, fw_rev = 0, s_id = 0;
  1440. int ret;
  1441. api_ver = s5k5baf_read(state, REG_FW_APIVER);
  1442. fw_rev = s5k5baf_read(state, REG_FW_REVISION) & 0xff;
  1443. s_id = s5k5baf_read(state, REG_FW_SENSOR_ID);
  1444. ret = s5k5baf_clear_error(state);
  1445. if (ret < 0)
  1446. return ret;
  1447. v4l2_info(&state->sd, "FW API=%#x, revision=%#x sensor_id=%#x\n",
  1448. api_ver, fw_rev, s_id);
  1449. if (api_ver != S5K5BAF_FW_APIVER) {
  1450. v4l2_err(&state->sd, "FW API version not supported\n");
  1451. return -ENODEV;
  1452. }
  1453. return 0;
  1454. }
  1455. static int s5k5baf_registered(struct v4l2_subdev *sd)
  1456. {
  1457. struct s5k5baf *state = to_s5k5baf(sd);
  1458. int ret;
  1459. ret = v4l2_device_register_subdev(sd->v4l2_dev, &state->cis_sd);
  1460. if (ret < 0)
  1461. v4l2_err(sd, "failed to register subdev %s\n",
  1462. state->cis_sd.name);
  1463. else
  1464. ret = media_entity_create_link(&state->cis_sd.entity, PAD_CIS,
  1465. &state->sd.entity, PAD_CIS,
  1466. MEDIA_LNK_FL_IMMUTABLE |
  1467. MEDIA_LNK_FL_ENABLED);
  1468. return ret;
  1469. }
  1470. static void s5k5baf_unregistered(struct v4l2_subdev *sd)
  1471. {
  1472. struct s5k5baf *state = to_s5k5baf(sd);
  1473. v4l2_device_unregister_subdev(&state->cis_sd);
  1474. }
  1475. static const struct v4l2_subdev_ops s5k5baf_cis_subdev_ops = {
  1476. .pad = &s5k5baf_cis_pad_ops,
  1477. };
  1478. static const struct v4l2_subdev_internal_ops s5k5baf_cis_subdev_internal_ops = {
  1479. .open = s5k5baf_open,
  1480. };
  1481. static const struct v4l2_subdev_internal_ops s5k5baf_subdev_internal_ops = {
  1482. .registered = s5k5baf_registered,
  1483. .unregistered = s5k5baf_unregistered,
  1484. .open = s5k5baf_open,
  1485. };
  1486. static const struct v4l2_subdev_core_ops s5k5baf_core_ops = {
  1487. .s_power = s5k5baf_set_power,
  1488. .log_status = v4l2_ctrl_subdev_log_status,
  1489. };
  1490. static const struct v4l2_subdev_ops s5k5baf_subdev_ops = {
  1491. .core = &s5k5baf_core_ops,
  1492. .pad = &s5k5baf_pad_ops,
  1493. .video = &s5k5baf_video_ops,
  1494. };
  1495. static int s5k5baf_configure_gpios(struct s5k5baf *state)
  1496. {
  1497. static const char const *name[] = { "S5K5BAF_STBY", "S5K5BAF_RST" };
  1498. struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
  1499. struct s5k5baf_gpio *g = state->gpios;
  1500. int ret, i;
  1501. for (i = 0; i < NUM_GPIOS; ++i) {
  1502. int flags = GPIOF_DIR_OUT;
  1503. if (g[i].level)
  1504. flags |= GPIOF_INIT_HIGH;
  1505. ret = devm_gpio_request_one(&c->dev, g[i].gpio, flags, name[i]);
  1506. if (ret < 0) {
  1507. v4l2_err(c, "failed to request gpio %s\n", name[i]);
  1508. return ret;
  1509. }
  1510. }
  1511. return 0;
  1512. }
  1513. static int s5k5baf_parse_gpios(struct s5k5baf_gpio *gpios, struct device *dev)
  1514. {
  1515. static const char * const names[] = {
  1516. "stbyn-gpios",
  1517. "rstn-gpios",
  1518. };
  1519. struct device_node *node = dev->of_node;
  1520. enum of_gpio_flags flags;
  1521. int ret, i;
  1522. for (i = 0; i < NUM_GPIOS; ++i) {
  1523. ret = of_get_named_gpio_flags(node, names[i], 0, &flags);
  1524. if (ret < 0) {
  1525. dev_err(dev, "no %s GPIO pin provided\n", names[i]);
  1526. return ret;
  1527. }
  1528. gpios[i].gpio = ret;
  1529. gpios[i].level = !(flags & OF_GPIO_ACTIVE_LOW);
  1530. }
  1531. return 0;
  1532. }
  1533. static int s5k5baf_parse_device_node(struct s5k5baf *state, struct device *dev)
  1534. {
  1535. struct device_node *node = dev->of_node;
  1536. struct device_node *node_ep;
  1537. struct v4l2_of_endpoint ep;
  1538. int ret;
  1539. if (!node) {
  1540. dev_err(dev, "no device-tree node provided\n");
  1541. return -EINVAL;
  1542. }
  1543. ret = of_property_read_u32(node, "clock-frequency",
  1544. &state->mclk_frequency);
  1545. if (ret < 0) {
  1546. state->mclk_frequency = S5K5BAF_DEFAULT_MCLK_FREQ;
  1547. dev_info(dev, "using default %u Hz clock frequency\n",
  1548. state->mclk_frequency);
  1549. }
  1550. ret = s5k5baf_parse_gpios(state->gpios, dev);
  1551. if (ret < 0)
  1552. return ret;
  1553. node_ep = of_graph_get_next_endpoint(node, NULL);
  1554. if (!node_ep) {
  1555. dev_err(dev, "no endpoint defined at node %s\n",
  1556. node->full_name);
  1557. return -EINVAL;
  1558. }
  1559. v4l2_of_parse_endpoint(node_ep, &ep);
  1560. of_node_put(node_ep);
  1561. state->bus_type = ep.bus_type;
  1562. switch (state->bus_type) {
  1563. case V4L2_MBUS_CSI2:
  1564. state->nlanes = ep.bus.mipi_csi2.num_data_lanes;
  1565. break;
  1566. case V4L2_MBUS_PARALLEL:
  1567. break;
  1568. default:
  1569. dev_err(dev, "unsupported bus in endpoint defined at node %s\n",
  1570. node->full_name);
  1571. return -EINVAL;
  1572. }
  1573. return 0;
  1574. }
  1575. static int s5k5baf_configure_subdevs(struct s5k5baf *state,
  1576. struct i2c_client *c)
  1577. {
  1578. struct v4l2_subdev *sd;
  1579. int ret;
  1580. sd = &state->cis_sd;
  1581. v4l2_subdev_init(sd, &s5k5baf_cis_subdev_ops);
  1582. sd->owner = THIS_MODULE;
  1583. v4l2_set_subdevdata(sd, state);
  1584. snprintf(sd->name, sizeof(sd->name), "S5K5BAF-CIS %d-%04x",
  1585. i2c_adapter_id(c->adapter), c->addr);
  1586. sd->internal_ops = &s5k5baf_cis_subdev_internal_ops;
  1587. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1588. state->cis_pad.flags = MEDIA_PAD_FL_SOURCE;
  1589. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
  1590. ret = media_entity_init(&sd->entity, NUM_CIS_PADS, &state->cis_pad, 0);
  1591. if (ret < 0)
  1592. goto err;
  1593. sd = &state->sd;
  1594. v4l2_i2c_subdev_init(sd, c, &s5k5baf_subdev_ops);
  1595. snprintf(sd->name, sizeof(sd->name), "S5K5BAF-ISP %d-%04x",
  1596. i2c_adapter_id(c->adapter), c->addr);
  1597. sd->internal_ops = &s5k5baf_subdev_internal_ops;
  1598. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1599. state->pads[PAD_CIS].flags = MEDIA_PAD_FL_SINK;
  1600. state->pads[PAD_OUT].flags = MEDIA_PAD_FL_SOURCE;
  1601. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
  1602. ret = media_entity_init(&sd->entity, NUM_ISP_PADS, state->pads, 0);
  1603. if (!ret)
  1604. return 0;
  1605. media_entity_cleanup(&state->cis_sd.entity);
  1606. err:
  1607. dev_err(&c->dev, "cannot init media entity %s\n", sd->name);
  1608. return ret;
  1609. }
  1610. static int s5k5baf_configure_regulators(struct s5k5baf *state)
  1611. {
  1612. struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
  1613. int ret;
  1614. int i;
  1615. for (i = 0; i < S5K5BAF_NUM_SUPPLIES; i++)
  1616. state->supplies[i].supply = s5k5baf_supply_names[i];
  1617. ret = devm_regulator_bulk_get(&c->dev, S5K5BAF_NUM_SUPPLIES,
  1618. state->supplies);
  1619. if (ret < 0)
  1620. v4l2_err(c, "failed to get regulators\n");
  1621. return ret;
  1622. }
  1623. static int s5k5baf_probe(struct i2c_client *c,
  1624. const struct i2c_device_id *id)
  1625. {
  1626. struct s5k5baf *state;
  1627. int ret;
  1628. state = devm_kzalloc(&c->dev, sizeof(*state), GFP_KERNEL);
  1629. if (!state)
  1630. return -ENOMEM;
  1631. mutex_init(&state->lock);
  1632. state->crop_sink = s5k5baf_cis_rect;
  1633. state->compose = s5k5baf_cis_rect;
  1634. state->crop_source = s5k5baf_cis_rect;
  1635. ret = s5k5baf_parse_device_node(state, &c->dev);
  1636. if (ret < 0)
  1637. return ret;
  1638. ret = s5k5baf_configure_subdevs(state, c);
  1639. if (ret < 0)
  1640. return ret;
  1641. ret = s5k5baf_configure_gpios(state);
  1642. if (ret < 0)
  1643. goto err_me;
  1644. ret = s5k5baf_configure_regulators(state);
  1645. if (ret < 0)
  1646. goto err_me;
  1647. state->clock = devm_clk_get(state->sd.dev, S5K5BAF_CLK_NAME);
  1648. if (IS_ERR(state->clock)) {
  1649. ret = -EPROBE_DEFER;
  1650. goto err_me;
  1651. }
  1652. ret = s5k5baf_power_on(state);
  1653. if (ret < 0) {
  1654. ret = -EPROBE_DEFER;
  1655. goto err_me;
  1656. }
  1657. s5k5baf_hw_init(state);
  1658. ret = s5k5baf_check_fw_revision(state);
  1659. s5k5baf_power_off(state);
  1660. if (ret < 0)
  1661. goto err_me;
  1662. ret = s5k5baf_initialize_ctrls(state);
  1663. if (ret < 0)
  1664. goto err_me;
  1665. ret = v4l2_async_register_subdev(&state->sd);
  1666. if (ret < 0)
  1667. goto err_ctrl;
  1668. return 0;
  1669. err_ctrl:
  1670. v4l2_ctrl_handler_free(state->sd.ctrl_handler);
  1671. err_me:
  1672. media_entity_cleanup(&state->sd.entity);
  1673. media_entity_cleanup(&state->cis_sd.entity);
  1674. return ret;
  1675. }
  1676. static int s5k5baf_remove(struct i2c_client *c)
  1677. {
  1678. struct v4l2_subdev *sd = i2c_get_clientdata(c);
  1679. struct s5k5baf *state = to_s5k5baf(sd);
  1680. v4l2_async_unregister_subdev(sd);
  1681. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1682. media_entity_cleanup(&sd->entity);
  1683. sd = &state->cis_sd;
  1684. v4l2_device_unregister_subdev(sd);
  1685. media_entity_cleanup(&sd->entity);
  1686. return 0;
  1687. }
  1688. static const struct i2c_device_id s5k5baf_id[] = {
  1689. { S5K5BAF_DRIVER_NAME, 0 },
  1690. { },
  1691. };
  1692. MODULE_DEVICE_TABLE(i2c, s5k5baf_id);
  1693. static const struct of_device_id s5k5baf_of_match[] = {
  1694. { .compatible = "samsung,s5k5baf" },
  1695. { }
  1696. };
  1697. MODULE_DEVICE_TABLE(of, s5k5baf_of_match);
  1698. static struct i2c_driver s5k5baf_i2c_driver = {
  1699. .driver = {
  1700. .of_match_table = s5k5baf_of_match,
  1701. .name = S5K5BAF_DRIVER_NAME
  1702. },
  1703. .probe = s5k5baf_probe,
  1704. .remove = s5k5baf_remove,
  1705. .id_table = s5k5baf_id,
  1706. };
  1707. module_i2c_driver(s5k5baf_i2c_driver);
  1708. MODULE_DESCRIPTION("Samsung S5K5BAF(X) UXGA camera driver");
  1709. MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
  1710. MODULE_LICENSE("GPL v2");