s5k5baf.c 50 KB

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