s5k6aa.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  1. /*
  2. * Driver for Samsung S5K6AAFX SXGA 1/6" 1.3M CMOS Image Sensor
  3. * with embedded SoC ISP.
  4. *
  5. * Copyright (C) 2011, Samsung Electronics Co., Ltd.
  6. * Sylwester Nawrocki <s.nawrocki@samsung.com>
  7. *
  8. * Based on a driver authored by Dongsoo Nathaniel Kim.
  9. * Copyright (C) 2009, Dongsoo Nathaniel Kim <dongsoo45.kim@samsung.com>
  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 as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/delay.h>
  18. #include <linux/gpio.h>
  19. #include <linux/i2c.h>
  20. #include <linux/media.h>
  21. #include <linux/module.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/slab.h>
  24. #include <media/media-entity.h>
  25. #include <media/v4l2-ctrls.h>
  26. #include <media/v4l2-device.h>
  27. #include <media/v4l2-subdev.h>
  28. #include <media/v4l2-mediabus.h>
  29. #include <media/i2c/s5k6aa.h>
  30. static int debug;
  31. module_param(debug, int, 0644);
  32. #define DRIVER_NAME "S5K6AA"
  33. /* The token to indicate array termination */
  34. #define S5K6AA_TERM 0xffff
  35. #define S5K6AA_OUT_WIDTH_DEF 640
  36. #define S5K6AA_OUT_HEIGHT_DEF 480
  37. #define S5K6AA_WIN_WIDTH_MAX 1280
  38. #define S5K6AA_WIN_HEIGHT_MAX 1024
  39. #define S5K6AA_WIN_WIDTH_MIN 8
  40. #define S5K6AA_WIN_HEIGHT_MIN 8
  41. /*
  42. * H/W register Interface (0xD0000000 - 0xD0000FFF)
  43. */
  44. #define AHB_MSB_ADDR_PTR 0xfcfc
  45. #define GEN_REG_OFFSH 0xd000
  46. #define REG_CMDWR_ADDRH 0x0028
  47. #define REG_CMDWR_ADDRL 0x002a
  48. #define REG_CMDRD_ADDRH 0x002c
  49. #define REG_CMDRD_ADDRL 0x002e
  50. #define REG_CMDBUF0_ADDR 0x0f12
  51. #define REG_CMDBUF1_ADDR 0x0f10
  52. /*
  53. * Host S/W Register interface (0x70000000 - 0x70002000)
  54. * The value of the two most significant address bytes is 0x7000,
  55. * (HOST_SWIF_OFFS_H). The register addresses below specify 2 LSBs.
  56. */
  57. #define HOST_SWIF_OFFSH 0x7000
  58. /* Initialization parameters */
  59. /* Master clock frequency in KHz */
  60. #define REG_I_INCLK_FREQ_L 0x01b8
  61. #define REG_I_INCLK_FREQ_H 0x01ba
  62. #define MIN_MCLK_FREQ_KHZ 6000U
  63. #define MAX_MCLK_FREQ_KHZ 27000U
  64. #define REG_I_USE_NPVI_CLOCKS 0x01c6
  65. #define REG_I_USE_NMIPI_CLOCKS 0x01c8
  66. /* Clock configurations, n = 0..2. REG_I_* frequency unit is 4 kHz. */
  67. #define REG_I_OPCLK_4KHZ(n) ((n) * 6 + 0x01cc)
  68. #define REG_I_MIN_OUTRATE_4KHZ(n) ((n) * 6 + 0x01ce)
  69. #define REG_I_MAX_OUTRATE_4KHZ(n) ((n) * 6 + 0x01d0)
  70. #define SYS_PLL_OUT_FREQ (48000000 / 4000)
  71. #define PCLK_FREQ_MIN (24000000 / 4000)
  72. #define PCLK_FREQ_MAX (48000000 / 4000)
  73. #define REG_I_INIT_PARAMS_UPDATED 0x01e0
  74. #define REG_I_ERROR_INFO 0x01e2
  75. /* General purpose parameters */
  76. #define REG_USER_BRIGHTNESS 0x01e4
  77. #define REG_USER_CONTRAST 0x01e6
  78. #define REG_USER_SATURATION 0x01e8
  79. #define REG_USER_SHARPBLUR 0x01ea
  80. #define REG_G_SPEC_EFFECTS 0x01ee
  81. #define REG_G_ENABLE_PREV 0x01f0
  82. #define REG_G_ENABLE_PREV_CHG 0x01f2
  83. #define REG_G_NEW_CFG_SYNC 0x01f8
  84. #define REG_G_PREVZOOM_IN_WIDTH 0x020a
  85. #define REG_G_PREVZOOM_IN_HEIGHT 0x020c
  86. #define REG_G_PREVZOOM_IN_XOFFS 0x020e
  87. #define REG_G_PREVZOOM_IN_YOFFS 0x0210
  88. #define REG_G_INPUTS_CHANGE_REQ 0x021a
  89. #define REG_G_ACTIVE_PREV_CFG 0x021c
  90. #define REG_G_PREV_CFG_CHG 0x021e
  91. #define REG_G_PREV_OPEN_AFTER_CH 0x0220
  92. #define REG_G_PREV_CFG_ERROR 0x0222
  93. /* Preview control section. n = 0...4. */
  94. #define PREG(n, x) ((n) * 0x26 + x)
  95. #define REG_P_OUT_WIDTH(n) PREG(n, 0x0242)
  96. #define REG_P_OUT_HEIGHT(n) PREG(n, 0x0244)
  97. #define REG_P_FMT(n) PREG(n, 0x0246)
  98. #define REG_P_MAX_OUT_RATE(n) PREG(n, 0x0248)
  99. #define REG_P_MIN_OUT_RATE(n) PREG(n, 0x024a)
  100. #define REG_P_PVI_MASK(n) PREG(n, 0x024c)
  101. #define REG_P_CLK_INDEX(n) PREG(n, 0x024e)
  102. #define REG_P_FR_RATE_TYPE(n) PREG(n, 0x0250)
  103. #define FR_RATE_DYNAMIC 0
  104. #define FR_RATE_FIXED 1
  105. #define FR_RATE_FIXED_ACCURATE 2
  106. #define REG_P_FR_RATE_Q_TYPE(n) PREG(n, 0x0252)
  107. #define FR_RATE_Q_BEST_FRRATE 1 /* Binning enabled */
  108. #define FR_RATE_Q_BEST_QUALITY 2 /* Binning disabled */
  109. /* Frame period in 0.1 ms units */
  110. #define REG_P_MAX_FR_TIME(n) PREG(n, 0x0254)
  111. #define REG_P_MIN_FR_TIME(n) PREG(n, 0x0256)
  112. /* Conversion to REG_P_[MAX/MIN]_FR_TIME value; __t: time in us */
  113. #define US_TO_FR_TIME(__t) ((__t) / 100)
  114. #define S5K6AA_MIN_FR_TIME 33300 /* us */
  115. #define S5K6AA_MAX_FR_TIME 650000 /* us */
  116. #define S5K6AA_MAX_HIGHRES_FR_TIME 666 /* x100 us */
  117. /* The below 5 registers are for "device correction" values */
  118. #define REG_P_COLORTEMP(n) PREG(n, 0x025e)
  119. #define REG_P_PREV_MIRROR(n) PREG(n, 0x0262)
  120. /* Extended image property controls */
  121. /* Exposure time in 10 us units */
  122. #define REG_SF_USR_EXPOSURE_L 0x03c6
  123. #define REG_SF_USR_EXPOSURE_H 0x03c8
  124. #define REG_SF_USR_EXPOSURE_CHG 0x03ca
  125. #define REG_SF_USR_TOT_GAIN 0x03cc
  126. #define REG_SF_USR_TOT_GAIN_CHG 0x03ce
  127. #define REG_SF_RGAIN 0x03d0
  128. #define REG_SF_RGAIN_CHG 0x03d2
  129. #define REG_SF_GGAIN 0x03d4
  130. #define REG_SF_GGAIN_CHG 0x03d6
  131. #define REG_SF_BGAIN 0x03d8
  132. #define REG_SF_BGAIN_CHG 0x03da
  133. #define REG_SF_FLICKER_QUANT 0x03dc
  134. #define REG_SF_FLICKER_QUANT_CHG 0x03de
  135. /* Output interface (parallel/MIPI) setup */
  136. #define REG_OIF_EN_MIPI_LANES 0x03fa
  137. #define REG_OIF_EN_PACKETS 0x03fc
  138. #define REG_OIF_CFG_CHG 0x03fe
  139. /* Auto-algorithms enable mask */
  140. #define REG_DBG_AUTOALG_EN 0x0400
  141. #define AALG_ALL_EN_MASK (1 << 0)
  142. #define AALG_AE_EN_MASK (1 << 1)
  143. #define AALG_DIVLEI_EN_MASK (1 << 2)
  144. #define AALG_WB_EN_MASK (1 << 3)
  145. #define AALG_FLICKER_EN_MASK (1 << 5)
  146. #define AALG_FIT_EN_MASK (1 << 6)
  147. #define AALG_WRHW_EN_MASK (1 << 7)
  148. /* Firmware revision information */
  149. #define REG_FW_APIVER 0x012e
  150. #define S5K6AAFX_FW_APIVER 0x0001
  151. #define REG_FW_REVISION 0x0130
  152. /* For now we use only one user configuration register set */
  153. #define S5K6AA_MAX_PRESETS 1
  154. static const char * const s5k6aa_supply_names[] = {
  155. "vdd_core", /* Digital core supply 1.5V (1.4V to 1.6V) */
  156. "vdda", /* Analog power supply 2.8V (2.6V to 3.0V) */
  157. "vdd_reg", /* Regulator input power 1.8V (1.7V to 1.9V)
  158. or 2.8V (2.6V to 3.0) */
  159. "vddio", /* I/O supply 1.8V (1.65V to 1.95V)
  160. or 2.8V (2.5V to 3.1V) */
  161. };
  162. #define S5K6AA_NUM_SUPPLIES ARRAY_SIZE(s5k6aa_supply_names)
  163. enum s5k6aa_gpio_id {
  164. STBY,
  165. RST,
  166. GPIO_NUM,
  167. };
  168. struct s5k6aa_regval {
  169. u16 addr;
  170. u16 val;
  171. };
  172. struct s5k6aa_pixfmt {
  173. u32 code;
  174. u32 colorspace;
  175. /* REG_P_FMT(x) register value */
  176. u16 reg_p_fmt;
  177. };
  178. struct s5k6aa_preset {
  179. /* output pixel format and resolution */
  180. struct v4l2_mbus_framefmt mbus_fmt;
  181. u8 clk_id;
  182. u8 index;
  183. };
  184. struct s5k6aa_ctrls {
  185. struct v4l2_ctrl_handler handler;
  186. /* Auto / manual white balance cluster */
  187. struct v4l2_ctrl *awb;
  188. struct v4l2_ctrl *gain_red;
  189. struct v4l2_ctrl *gain_blue;
  190. struct v4l2_ctrl *gain_green;
  191. /* Mirror cluster */
  192. struct v4l2_ctrl *hflip;
  193. struct v4l2_ctrl *vflip;
  194. /* Auto exposure / manual exposure and gain cluster */
  195. struct v4l2_ctrl *auto_exp;
  196. struct v4l2_ctrl *exposure;
  197. struct v4l2_ctrl *gain;
  198. };
  199. struct s5k6aa_interval {
  200. u16 reg_fr_time;
  201. struct v4l2_fract interval;
  202. /* Maximum rectangle for the interval */
  203. struct v4l2_frmsize_discrete size;
  204. };
  205. struct s5k6aa {
  206. struct v4l2_subdev sd;
  207. struct media_pad pad;
  208. enum v4l2_mbus_type bus_type;
  209. u8 mipi_lanes;
  210. int (*s_power)(int enable);
  211. struct regulator_bulk_data supplies[S5K6AA_NUM_SUPPLIES];
  212. struct s5k6aa_gpio gpio[GPIO_NUM];
  213. /* external master clock frequency */
  214. unsigned long mclk_frequency;
  215. /* ISP internal master clock frequency */
  216. u16 clk_fop;
  217. /* output pixel clock frequency range */
  218. u16 pclk_fmin;
  219. u16 pclk_fmax;
  220. unsigned int inv_hflip:1;
  221. unsigned int inv_vflip:1;
  222. /* protects the struct members below */
  223. struct mutex lock;
  224. /* sensor matrix scan window */
  225. struct v4l2_rect ccd_rect;
  226. struct s5k6aa_ctrls ctrls;
  227. struct s5k6aa_preset presets[S5K6AA_MAX_PRESETS];
  228. struct s5k6aa_preset *preset;
  229. const struct s5k6aa_interval *fiv;
  230. unsigned int streaming:1;
  231. unsigned int apply_cfg:1;
  232. unsigned int apply_crop:1;
  233. unsigned int power;
  234. };
  235. static struct s5k6aa_regval s5k6aa_analog_config[] = {
  236. /* Analog settings */
  237. { 0x112a, 0x0000 }, { 0x1132, 0x0000 },
  238. { 0x113e, 0x0000 }, { 0x115c, 0x0000 },
  239. { 0x1164, 0x0000 }, { 0x1174, 0x0000 },
  240. { 0x1178, 0x0000 }, { 0x077a, 0x0000 },
  241. { 0x077c, 0x0000 }, { 0x077e, 0x0000 },
  242. { 0x0780, 0x0000 }, { 0x0782, 0x0000 },
  243. { 0x0784, 0x0000 }, { 0x0786, 0x0000 },
  244. { 0x0788, 0x0000 }, { 0x07a2, 0x0000 },
  245. { 0x07a4, 0x0000 }, { 0x07a6, 0x0000 },
  246. { 0x07a8, 0x0000 }, { 0x07b6, 0x0000 },
  247. { 0x07b8, 0x0002 }, { 0x07ba, 0x0004 },
  248. { 0x07bc, 0x0004 }, { 0x07be, 0x0005 },
  249. { 0x07c0, 0x0005 }, { S5K6AA_TERM, 0 },
  250. };
  251. /* TODO: Add RGB888 and Bayer format */
  252. static const struct s5k6aa_pixfmt s5k6aa_formats[] = {
  253. { MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG, 5 },
  254. /* range 16-240 */
  255. { MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_REC709, 6 },
  256. { MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_JPEG, 0 },
  257. };
  258. static const struct s5k6aa_interval s5k6aa_intervals[] = {
  259. { 1000, {10000, 1000000}, {1280, 1024} }, /* 10 fps */
  260. { 666, {15000, 1000000}, {1280, 1024} }, /* 15 fps */
  261. { 500, {20000, 1000000}, {1280, 720} }, /* 20 fps */
  262. { 400, {25000, 1000000}, {640, 480} }, /* 25 fps */
  263. { 333, {33300, 1000000}, {640, 480} }, /* 30 fps */
  264. };
  265. #define S5K6AA_INTERVAL_DEF_INDEX 1 /* 15 fps */
  266. static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
  267. {
  268. return &container_of(ctrl->handler, struct s5k6aa, ctrls.handler)->sd;
  269. }
  270. static inline struct s5k6aa *to_s5k6aa(struct v4l2_subdev *sd)
  271. {
  272. return container_of(sd, struct s5k6aa, sd);
  273. }
  274. /* Set initial values for all preview presets */
  275. static void s5k6aa_presets_data_init(struct s5k6aa *s5k6aa)
  276. {
  277. struct s5k6aa_preset *preset = &s5k6aa->presets[0];
  278. int i;
  279. for (i = 0; i < S5K6AA_MAX_PRESETS; i++) {
  280. preset->mbus_fmt.width = S5K6AA_OUT_WIDTH_DEF;
  281. preset->mbus_fmt.height = S5K6AA_OUT_HEIGHT_DEF;
  282. preset->mbus_fmt.code = s5k6aa_formats[0].code;
  283. preset->index = i;
  284. preset->clk_id = 0;
  285. preset++;
  286. }
  287. s5k6aa->fiv = &s5k6aa_intervals[S5K6AA_INTERVAL_DEF_INDEX];
  288. s5k6aa->preset = &s5k6aa->presets[0];
  289. }
  290. static int s5k6aa_i2c_read(struct i2c_client *client, u16 addr, u16 *val)
  291. {
  292. u8 wbuf[2] = {addr >> 8, addr & 0xFF};
  293. struct i2c_msg msg[2];
  294. u8 rbuf[2];
  295. int ret;
  296. msg[0].addr = client->addr;
  297. msg[0].flags = 0;
  298. msg[0].len = 2;
  299. msg[0].buf = wbuf;
  300. msg[1].addr = client->addr;
  301. msg[1].flags = I2C_M_RD;
  302. msg[1].len = 2;
  303. msg[1].buf = rbuf;
  304. ret = i2c_transfer(client->adapter, msg, 2);
  305. *val = be16_to_cpu(*((__be16 *)rbuf));
  306. v4l2_dbg(3, debug, client, "i2c_read: 0x%04X : 0x%04x\n", addr, *val);
  307. return ret == 2 ? 0 : ret;
  308. }
  309. static int s5k6aa_i2c_write(struct i2c_client *client, u16 addr, u16 val)
  310. {
  311. u8 buf[4] = {addr >> 8, addr & 0xFF, val >> 8, val & 0xFF};
  312. int ret = i2c_master_send(client, buf, 4);
  313. v4l2_dbg(3, debug, client, "i2c_write: 0x%04X : 0x%04x\n", addr, val);
  314. return ret == 4 ? 0 : ret;
  315. }
  316. /* The command register write, assumes Command_Wr_addH = 0x7000. */
  317. static int s5k6aa_write(struct i2c_client *c, u16 addr, u16 val)
  318. {
  319. int ret = s5k6aa_i2c_write(c, REG_CMDWR_ADDRL, addr);
  320. if (ret)
  321. return ret;
  322. return s5k6aa_i2c_write(c, REG_CMDBUF0_ADDR, val);
  323. }
  324. /* The command register read, assumes Command_Rd_addH = 0x7000. */
  325. static int s5k6aa_read(struct i2c_client *client, u16 addr, u16 *val)
  326. {
  327. int ret = s5k6aa_i2c_write(client, REG_CMDRD_ADDRL, addr);
  328. if (ret)
  329. return ret;
  330. return s5k6aa_i2c_read(client, REG_CMDBUF0_ADDR, val);
  331. }
  332. static int s5k6aa_write_array(struct v4l2_subdev *sd,
  333. const struct s5k6aa_regval *msg)
  334. {
  335. struct i2c_client *client = v4l2_get_subdevdata(sd);
  336. u16 addr_incr = 0;
  337. int ret = 0;
  338. while (msg->addr != S5K6AA_TERM) {
  339. if (addr_incr != 2)
  340. ret = s5k6aa_i2c_write(client, REG_CMDWR_ADDRL,
  341. msg->addr);
  342. if (ret)
  343. break;
  344. ret = s5k6aa_i2c_write(client, REG_CMDBUF0_ADDR, msg->val);
  345. if (ret)
  346. break;
  347. /* Assume that msg->addr is always less than 0xfffc */
  348. addr_incr = (msg + 1)->addr - msg->addr;
  349. msg++;
  350. }
  351. return ret;
  352. }
  353. /* Configure the AHB high address bytes for GTG registers access */
  354. static int s5k6aa_set_ahb_address(struct i2c_client *client)
  355. {
  356. int ret = s5k6aa_i2c_write(client, AHB_MSB_ADDR_PTR, GEN_REG_OFFSH);
  357. if (ret)
  358. return ret;
  359. ret = s5k6aa_i2c_write(client, REG_CMDRD_ADDRH, HOST_SWIF_OFFSH);
  360. if (ret)
  361. return ret;
  362. return s5k6aa_i2c_write(client, REG_CMDWR_ADDRH, HOST_SWIF_OFFSH);
  363. }
  364. /**
  365. * s5k6aa_configure_pixel_clock - apply ISP main clock/PLL configuration
  366. *
  367. * Configure the internal ISP PLL for the required output frequency.
  368. * Locking: called with s5k6aa.lock mutex held.
  369. */
  370. static int s5k6aa_configure_pixel_clocks(struct s5k6aa *s5k6aa)
  371. {
  372. struct i2c_client *c = v4l2_get_subdevdata(&s5k6aa->sd);
  373. unsigned long fmclk = s5k6aa->mclk_frequency / 1000;
  374. u16 status;
  375. int ret;
  376. if (WARN(fmclk < MIN_MCLK_FREQ_KHZ || fmclk > MAX_MCLK_FREQ_KHZ,
  377. "Invalid clock frequency: %ld\n", fmclk))
  378. return -EINVAL;
  379. s5k6aa->pclk_fmin = PCLK_FREQ_MIN;
  380. s5k6aa->pclk_fmax = PCLK_FREQ_MAX;
  381. s5k6aa->clk_fop = SYS_PLL_OUT_FREQ;
  382. /* External input clock frequency in kHz */
  383. ret = s5k6aa_write(c, REG_I_INCLK_FREQ_H, fmclk >> 16);
  384. if (!ret)
  385. ret = s5k6aa_write(c, REG_I_INCLK_FREQ_L, fmclk & 0xFFFF);
  386. if (!ret)
  387. ret = s5k6aa_write(c, REG_I_USE_NPVI_CLOCKS, 1);
  388. /* Internal PLL frequency */
  389. if (!ret)
  390. ret = s5k6aa_write(c, REG_I_OPCLK_4KHZ(0), s5k6aa->clk_fop);
  391. if (!ret)
  392. ret = s5k6aa_write(c, REG_I_MIN_OUTRATE_4KHZ(0),
  393. s5k6aa->pclk_fmin);
  394. if (!ret)
  395. ret = s5k6aa_write(c, REG_I_MAX_OUTRATE_4KHZ(0),
  396. s5k6aa->pclk_fmax);
  397. if (!ret)
  398. ret = s5k6aa_write(c, REG_I_INIT_PARAMS_UPDATED, 1);
  399. if (!ret)
  400. ret = s5k6aa_read(c, REG_I_ERROR_INFO, &status);
  401. return ret ? ret : (status ? -EINVAL : 0);
  402. }
  403. /* Set horizontal and vertical image flipping */
  404. static int s5k6aa_set_mirror(struct s5k6aa *s5k6aa, int horiz_flip)
  405. {
  406. struct i2c_client *client = v4l2_get_subdevdata(&s5k6aa->sd);
  407. int index = s5k6aa->preset->index;
  408. unsigned int vflip = s5k6aa->ctrls.vflip->val ^ s5k6aa->inv_vflip;
  409. unsigned int flip = (horiz_flip ^ s5k6aa->inv_hflip) | (vflip << 1);
  410. return s5k6aa_write(client, REG_P_PREV_MIRROR(index), flip);
  411. }
  412. /* Configure auto/manual white balance and R/G/B gains */
  413. static int s5k6aa_set_awb(struct s5k6aa *s5k6aa, int awb)
  414. {
  415. struct i2c_client *c = v4l2_get_subdevdata(&s5k6aa->sd);
  416. struct s5k6aa_ctrls *ctrls = &s5k6aa->ctrls;
  417. u16 reg;
  418. int ret = s5k6aa_read(c, REG_DBG_AUTOALG_EN, &reg);
  419. if (!ret && !awb) {
  420. ret = s5k6aa_write(c, REG_SF_RGAIN, ctrls->gain_red->val);
  421. if (!ret)
  422. ret = s5k6aa_write(c, REG_SF_RGAIN_CHG, 1);
  423. if (ret)
  424. return ret;
  425. ret = s5k6aa_write(c, REG_SF_GGAIN, ctrls->gain_green->val);
  426. if (!ret)
  427. ret = s5k6aa_write(c, REG_SF_GGAIN_CHG, 1);
  428. if (ret)
  429. return ret;
  430. ret = s5k6aa_write(c, REG_SF_BGAIN, ctrls->gain_blue->val);
  431. if (!ret)
  432. ret = s5k6aa_write(c, REG_SF_BGAIN_CHG, 1);
  433. }
  434. if (!ret) {
  435. reg = awb ? reg | AALG_WB_EN_MASK : reg & ~AALG_WB_EN_MASK;
  436. ret = s5k6aa_write(c, REG_DBG_AUTOALG_EN, reg);
  437. }
  438. return ret;
  439. }
  440. /* Program FW with exposure time, 'exposure' in us units */
  441. static int s5k6aa_set_user_exposure(struct i2c_client *client, int exposure)
  442. {
  443. unsigned int time = exposure / 10;
  444. int ret = s5k6aa_write(client, REG_SF_USR_EXPOSURE_L, time & 0xffff);
  445. if (!ret)
  446. ret = s5k6aa_write(client, REG_SF_USR_EXPOSURE_H, time >> 16);
  447. if (ret)
  448. return ret;
  449. return s5k6aa_write(client, REG_SF_USR_EXPOSURE_CHG, 1);
  450. }
  451. static int s5k6aa_set_user_gain(struct i2c_client *client, int gain)
  452. {
  453. int ret = s5k6aa_write(client, REG_SF_USR_TOT_GAIN, gain);
  454. if (ret)
  455. return ret;
  456. return s5k6aa_write(client, REG_SF_USR_TOT_GAIN_CHG, 1);
  457. }
  458. /* Set auto/manual exposure and total gain */
  459. static int s5k6aa_set_auto_exposure(struct s5k6aa *s5k6aa, int value)
  460. {
  461. struct i2c_client *c = v4l2_get_subdevdata(&s5k6aa->sd);
  462. unsigned int exp_time = s5k6aa->ctrls.exposure->val;
  463. u16 auto_alg;
  464. int ret = s5k6aa_read(c, REG_DBG_AUTOALG_EN, &auto_alg);
  465. if (ret)
  466. return ret;
  467. v4l2_dbg(1, debug, c, "man_exp: %d, auto_exp: %d, a_alg: 0x%x\n",
  468. exp_time, value, auto_alg);
  469. if (value == V4L2_EXPOSURE_AUTO) {
  470. auto_alg |= AALG_AE_EN_MASK | AALG_DIVLEI_EN_MASK;
  471. } else {
  472. ret = s5k6aa_set_user_exposure(c, exp_time);
  473. if (ret)
  474. return ret;
  475. ret = s5k6aa_set_user_gain(c, s5k6aa->ctrls.gain->val);
  476. if (ret)
  477. return ret;
  478. auto_alg &= ~(AALG_AE_EN_MASK | AALG_DIVLEI_EN_MASK);
  479. }
  480. return s5k6aa_write(c, REG_DBG_AUTOALG_EN, auto_alg);
  481. }
  482. static int s5k6aa_set_anti_flicker(struct s5k6aa *s5k6aa, int value)
  483. {
  484. struct i2c_client *client = v4l2_get_subdevdata(&s5k6aa->sd);
  485. u16 auto_alg;
  486. int ret;
  487. ret = s5k6aa_read(client, REG_DBG_AUTOALG_EN, &auto_alg);
  488. if (ret)
  489. return ret;
  490. if (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) {
  491. auto_alg |= AALG_FLICKER_EN_MASK;
  492. } else {
  493. auto_alg &= ~AALG_FLICKER_EN_MASK;
  494. /* The V4L2_CID_LINE_FREQUENCY control values match
  495. * the register values */
  496. ret = s5k6aa_write(client, REG_SF_FLICKER_QUANT, value);
  497. if (ret)
  498. return ret;
  499. ret = s5k6aa_write(client, REG_SF_FLICKER_QUANT_CHG, 1);
  500. if (ret)
  501. return ret;
  502. }
  503. return s5k6aa_write(client, REG_DBG_AUTOALG_EN, auto_alg);
  504. }
  505. static int s5k6aa_set_colorfx(struct s5k6aa *s5k6aa, int val)
  506. {
  507. struct i2c_client *client = v4l2_get_subdevdata(&s5k6aa->sd);
  508. static const struct v4l2_control colorfx[] = {
  509. { V4L2_COLORFX_NONE, 0 },
  510. { V4L2_COLORFX_BW, 1 },
  511. { V4L2_COLORFX_NEGATIVE, 2 },
  512. { V4L2_COLORFX_SEPIA, 3 },
  513. { V4L2_COLORFX_SKY_BLUE, 4 },
  514. { V4L2_COLORFX_SKETCH, 5 },
  515. };
  516. int i;
  517. for (i = 0; i < ARRAY_SIZE(colorfx); i++) {
  518. if (colorfx[i].id == val)
  519. return s5k6aa_write(client, REG_G_SPEC_EFFECTS,
  520. colorfx[i].value);
  521. }
  522. return -EINVAL;
  523. }
  524. static int s5k6aa_preview_config_status(struct i2c_client *client)
  525. {
  526. u16 error = 0;
  527. int ret = s5k6aa_read(client, REG_G_PREV_CFG_ERROR, &error);
  528. v4l2_dbg(1, debug, client, "error: 0x%x (%d)\n", error, ret);
  529. return ret ? ret : (error ? -EINVAL : 0);
  530. }
  531. static int s5k6aa_get_pixfmt_index(struct s5k6aa *s5k6aa,
  532. struct v4l2_mbus_framefmt *mf)
  533. {
  534. unsigned int i;
  535. for (i = 0; i < ARRAY_SIZE(s5k6aa_formats); i++)
  536. if (mf->colorspace == s5k6aa_formats[i].colorspace &&
  537. mf->code == s5k6aa_formats[i].code)
  538. return i;
  539. return 0;
  540. }
  541. static int s5k6aa_set_output_framefmt(struct s5k6aa *s5k6aa,
  542. struct s5k6aa_preset *preset)
  543. {
  544. struct i2c_client *client = v4l2_get_subdevdata(&s5k6aa->sd);
  545. int fmt_index = s5k6aa_get_pixfmt_index(s5k6aa, &preset->mbus_fmt);
  546. int ret;
  547. ret = s5k6aa_write(client, REG_P_OUT_WIDTH(preset->index),
  548. preset->mbus_fmt.width);
  549. if (!ret)
  550. ret = s5k6aa_write(client, REG_P_OUT_HEIGHT(preset->index),
  551. preset->mbus_fmt.height);
  552. if (!ret)
  553. ret = s5k6aa_write(client, REG_P_FMT(preset->index),
  554. s5k6aa_formats[fmt_index].reg_p_fmt);
  555. return ret;
  556. }
  557. static int s5k6aa_set_input_params(struct s5k6aa *s5k6aa)
  558. {
  559. struct i2c_client *c = v4l2_get_subdevdata(&s5k6aa->sd);
  560. struct v4l2_rect *r = &s5k6aa->ccd_rect;
  561. int ret;
  562. ret = s5k6aa_write(c, REG_G_PREVZOOM_IN_WIDTH, r->width);
  563. if (!ret)
  564. ret = s5k6aa_write(c, REG_G_PREVZOOM_IN_HEIGHT, r->height);
  565. if (!ret)
  566. ret = s5k6aa_write(c, REG_G_PREVZOOM_IN_XOFFS, r->left);
  567. if (!ret)
  568. ret = s5k6aa_write(c, REG_G_PREVZOOM_IN_YOFFS, r->top);
  569. if (!ret)
  570. ret = s5k6aa_write(c, REG_G_INPUTS_CHANGE_REQ, 1);
  571. if (!ret)
  572. s5k6aa->apply_crop = 0;
  573. return ret;
  574. }
  575. /**
  576. * s5k6aa_configure_video_bus - configure the video output interface
  577. * @bus_type: video bus type: parallel or MIPI-CSI
  578. * @nlanes: number of MIPI lanes to be used (MIPI-CSI only)
  579. *
  580. * Note: Only parallel bus operation has been tested.
  581. */
  582. static int s5k6aa_configure_video_bus(struct s5k6aa *s5k6aa,
  583. enum v4l2_mbus_type bus_type, int nlanes)
  584. {
  585. struct i2c_client *client = v4l2_get_subdevdata(&s5k6aa->sd);
  586. u16 cfg = 0;
  587. int ret;
  588. /*
  589. * TODO: The sensor is supposed to support BT.601 and BT.656
  590. * but there is nothing indicating how to switch between both
  591. * in the datasheet. For now default BT.601 interface is assumed.
  592. */
  593. if (bus_type == V4L2_MBUS_CSI2)
  594. cfg = nlanes;
  595. else if (bus_type != V4L2_MBUS_PARALLEL)
  596. return -EINVAL;
  597. ret = s5k6aa_write(client, REG_OIF_EN_MIPI_LANES, cfg);
  598. if (ret)
  599. return ret;
  600. return s5k6aa_write(client, REG_OIF_CFG_CHG, 1);
  601. }
  602. /* This function should be called when switching to new user configuration set*/
  603. static int s5k6aa_new_config_sync(struct i2c_client *client, int timeout,
  604. int cid)
  605. {
  606. unsigned long end = jiffies + msecs_to_jiffies(timeout);
  607. u16 reg = 1;
  608. int ret;
  609. ret = s5k6aa_write(client, REG_G_ACTIVE_PREV_CFG, cid);
  610. if (!ret)
  611. ret = s5k6aa_write(client, REG_G_PREV_CFG_CHG, 1);
  612. if (!ret)
  613. ret = s5k6aa_write(client, REG_G_NEW_CFG_SYNC, 1);
  614. if (timeout == 0)
  615. return ret;
  616. while (ret >= 0 && time_is_after_jiffies(end)) {
  617. ret = s5k6aa_read(client, REG_G_NEW_CFG_SYNC, &reg);
  618. if (!reg)
  619. return 0;
  620. usleep_range(1000, 5000);
  621. }
  622. return ret ? ret : -ETIMEDOUT;
  623. }
  624. /**
  625. * s5k6aa_set_prev_config - write user preview register set
  626. *
  627. * Configure output resolution and color fromat, pixel clock
  628. * frequency range, device frame rate type and frame period range.
  629. */
  630. static int s5k6aa_set_prev_config(struct s5k6aa *s5k6aa,
  631. struct s5k6aa_preset *preset)
  632. {
  633. struct i2c_client *client = v4l2_get_subdevdata(&s5k6aa->sd);
  634. int idx = preset->index;
  635. u16 frame_rate_q;
  636. int ret;
  637. if (s5k6aa->fiv->reg_fr_time >= S5K6AA_MAX_HIGHRES_FR_TIME)
  638. frame_rate_q = FR_RATE_Q_BEST_FRRATE;
  639. else
  640. frame_rate_q = FR_RATE_Q_BEST_QUALITY;
  641. ret = s5k6aa_set_output_framefmt(s5k6aa, preset);
  642. if (!ret)
  643. ret = s5k6aa_write(client, REG_P_MAX_OUT_RATE(idx),
  644. s5k6aa->pclk_fmax);
  645. if (!ret)
  646. ret = s5k6aa_write(client, REG_P_MIN_OUT_RATE(idx),
  647. s5k6aa->pclk_fmin);
  648. if (!ret)
  649. ret = s5k6aa_write(client, REG_P_CLK_INDEX(idx),
  650. preset->clk_id);
  651. if (!ret)
  652. ret = s5k6aa_write(client, REG_P_FR_RATE_TYPE(idx),
  653. FR_RATE_DYNAMIC);
  654. if (!ret)
  655. ret = s5k6aa_write(client, REG_P_FR_RATE_Q_TYPE(idx),
  656. frame_rate_q);
  657. if (!ret)
  658. ret = s5k6aa_write(client, REG_P_MAX_FR_TIME(idx),
  659. s5k6aa->fiv->reg_fr_time + 33);
  660. if (!ret)
  661. ret = s5k6aa_write(client, REG_P_MIN_FR_TIME(idx),
  662. s5k6aa->fiv->reg_fr_time - 33);
  663. if (!ret)
  664. ret = s5k6aa_new_config_sync(client, 250, idx);
  665. if (!ret)
  666. ret = s5k6aa_preview_config_status(client);
  667. if (!ret)
  668. s5k6aa->apply_cfg = 0;
  669. v4l2_dbg(1, debug, client, "Frame interval: %d +/- 3.3ms. (%d)\n",
  670. s5k6aa->fiv->reg_fr_time, ret);
  671. return ret;
  672. }
  673. /**
  674. * s5k6aa_initialize_isp - basic ISP MCU initialization
  675. *
  676. * Configure AHB addresses for registers read/write; configure PLLs for
  677. * required output pixel clock. The ISP power supply needs to be already
  678. * enabled, with an optional H/W reset.
  679. * Locking: called with s5k6aa.lock mutex held.
  680. */
  681. static int s5k6aa_initialize_isp(struct v4l2_subdev *sd)
  682. {
  683. struct i2c_client *client = v4l2_get_subdevdata(sd);
  684. struct s5k6aa *s5k6aa = to_s5k6aa(sd);
  685. int ret;
  686. s5k6aa->apply_crop = 1;
  687. s5k6aa->apply_cfg = 1;
  688. msleep(100);
  689. ret = s5k6aa_set_ahb_address(client);
  690. if (ret)
  691. return ret;
  692. ret = s5k6aa_configure_video_bus(s5k6aa, s5k6aa->bus_type,
  693. s5k6aa->mipi_lanes);
  694. if (ret)
  695. return ret;
  696. ret = s5k6aa_write_array(sd, s5k6aa_analog_config);
  697. if (ret)
  698. return ret;
  699. msleep(20);
  700. return s5k6aa_configure_pixel_clocks(s5k6aa);
  701. }
  702. static int s5k6aa_gpio_set_value(struct s5k6aa *priv, int id, u32 val)
  703. {
  704. if (!gpio_is_valid(priv->gpio[id].gpio))
  705. return 0;
  706. gpio_set_value(priv->gpio[id].gpio, !!val);
  707. return 1;
  708. }
  709. static int s5k6aa_gpio_assert(struct s5k6aa *priv, int id)
  710. {
  711. return s5k6aa_gpio_set_value(priv, id, priv->gpio[id].level);
  712. }
  713. static int s5k6aa_gpio_deassert(struct s5k6aa *priv, int id)
  714. {
  715. return s5k6aa_gpio_set_value(priv, id, !priv->gpio[id].level);
  716. }
  717. static int __s5k6aa_power_on(struct s5k6aa *s5k6aa)
  718. {
  719. int ret;
  720. ret = regulator_bulk_enable(S5K6AA_NUM_SUPPLIES, s5k6aa->supplies);
  721. if (ret)
  722. return ret;
  723. if (s5k6aa_gpio_deassert(s5k6aa, STBY))
  724. usleep_range(150, 200);
  725. if (s5k6aa->s_power)
  726. ret = s5k6aa->s_power(1);
  727. usleep_range(4000, 4000);
  728. if (s5k6aa_gpio_deassert(s5k6aa, RST))
  729. msleep(20);
  730. return ret;
  731. }
  732. static int __s5k6aa_power_off(struct s5k6aa *s5k6aa)
  733. {
  734. int ret;
  735. if (s5k6aa_gpio_assert(s5k6aa, RST))
  736. usleep_range(100, 150);
  737. if (s5k6aa->s_power) {
  738. ret = s5k6aa->s_power(0);
  739. if (ret)
  740. return ret;
  741. }
  742. if (s5k6aa_gpio_assert(s5k6aa, STBY))
  743. usleep_range(50, 100);
  744. s5k6aa->streaming = 0;
  745. return regulator_bulk_disable(S5K6AA_NUM_SUPPLIES, s5k6aa->supplies);
  746. }
  747. /*
  748. * V4L2 subdev core and video operations
  749. */
  750. static int s5k6aa_set_power(struct v4l2_subdev *sd, int on)
  751. {
  752. struct s5k6aa *s5k6aa = to_s5k6aa(sd);
  753. int ret = 0;
  754. mutex_lock(&s5k6aa->lock);
  755. if (s5k6aa->power == !on) {
  756. if (on) {
  757. ret = __s5k6aa_power_on(s5k6aa);
  758. if (!ret)
  759. ret = s5k6aa_initialize_isp(sd);
  760. } else {
  761. ret = __s5k6aa_power_off(s5k6aa);
  762. }
  763. if (!ret)
  764. s5k6aa->power += on ? 1 : -1;
  765. }
  766. mutex_unlock(&s5k6aa->lock);
  767. if (!on || ret || s5k6aa->power != 1)
  768. return ret;
  769. return v4l2_ctrl_handler_setup(sd->ctrl_handler);
  770. }
  771. static int __s5k6aa_stream(struct s5k6aa *s5k6aa, int enable)
  772. {
  773. struct i2c_client *client = v4l2_get_subdevdata(&s5k6aa->sd);
  774. int ret = 0;
  775. ret = s5k6aa_write(client, REG_G_ENABLE_PREV, enable);
  776. if (!ret)
  777. ret = s5k6aa_write(client, REG_G_ENABLE_PREV_CHG, 1);
  778. if (!ret)
  779. s5k6aa->streaming = enable;
  780. return ret;
  781. }
  782. static int s5k6aa_s_stream(struct v4l2_subdev *sd, int on)
  783. {
  784. struct s5k6aa *s5k6aa = to_s5k6aa(sd);
  785. int ret = 0;
  786. mutex_lock(&s5k6aa->lock);
  787. if (s5k6aa->streaming == !on) {
  788. if (!ret && s5k6aa->apply_cfg)
  789. ret = s5k6aa_set_prev_config(s5k6aa, s5k6aa->preset);
  790. if (s5k6aa->apply_crop)
  791. ret = s5k6aa_set_input_params(s5k6aa);
  792. if (!ret)
  793. ret = __s5k6aa_stream(s5k6aa, !!on);
  794. }
  795. mutex_unlock(&s5k6aa->lock);
  796. return ret;
  797. }
  798. static int s5k6aa_g_frame_interval(struct v4l2_subdev *sd,
  799. struct v4l2_subdev_frame_interval *fi)
  800. {
  801. struct s5k6aa *s5k6aa = to_s5k6aa(sd);
  802. mutex_lock(&s5k6aa->lock);
  803. fi->interval = s5k6aa->fiv->interval;
  804. mutex_unlock(&s5k6aa->lock);
  805. return 0;
  806. }
  807. static int __s5k6aa_set_frame_interval(struct s5k6aa *s5k6aa,
  808. struct v4l2_subdev_frame_interval *fi)
  809. {
  810. struct v4l2_mbus_framefmt *mbus_fmt = &s5k6aa->preset->mbus_fmt;
  811. const struct s5k6aa_interval *fiv = &s5k6aa_intervals[0];
  812. unsigned int err, min_err = UINT_MAX;
  813. unsigned int i, fr_time;
  814. if (fi->interval.denominator == 0)
  815. return -EINVAL;
  816. fr_time = fi->interval.numerator * 10000 / fi->interval.denominator;
  817. for (i = 0; i < ARRAY_SIZE(s5k6aa_intervals); i++) {
  818. const struct s5k6aa_interval *iv = &s5k6aa_intervals[i];
  819. if (mbus_fmt->width > iv->size.width ||
  820. mbus_fmt->height > iv->size.height)
  821. continue;
  822. err = abs(iv->reg_fr_time - fr_time);
  823. if (err < min_err) {
  824. fiv = iv;
  825. min_err = err;
  826. }
  827. }
  828. s5k6aa->fiv = fiv;
  829. v4l2_dbg(1, debug, &s5k6aa->sd, "Changed frame interval to %d us\n",
  830. fiv->reg_fr_time * 100);
  831. return 0;
  832. }
  833. static int s5k6aa_s_frame_interval(struct v4l2_subdev *sd,
  834. struct v4l2_subdev_frame_interval *fi)
  835. {
  836. struct s5k6aa *s5k6aa = to_s5k6aa(sd);
  837. int ret;
  838. v4l2_dbg(1, debug, sd, "Setting %d/%d frame interval\n",
  839. fi->interval.numerator, fi->interval.denominator);
  840. mutex_lock(&s5k6aa->lock);
  841. ret = __s5k6aa_set_frame_interval(s5k6aa, fi);
  842. s5k6aa->apply_cfg = 1;
  843. mutex_unlock(&s5k6aa->lock);
  844. return ret;
  845. }
  846. /*
  847. * V4L2 subdev pad level and video operations
  848. */
  849. static int s5k6aa_enum_frame_interval(struct v4l2_subdev *sd,
  850. struct v4l2_subdev_pad_config *cfg,
  851. struct v4l2_subdev_frame_interval_enum *fie)
  852. {
  853. struct s5k6aa *s5k6aa = to_s5k6aa(sd);
  854. const struct s5k6aa_interval *fi;
  855. int ret = 0;
  856. if (fie->index >= ARRAY_SIZE(s5k6aa_intervals))
  857. return -EINVAL;
  858. v4l_bound_align_image(&fie->width, S5K6AA_WIN_WIDTH_MIN,
  859. S5K6AA_WIN_WIDTH_MAX, 1,
  860. &fie->height, S5K6AA_WIN_HEIGHT_MIN,
  861. S5K6AA_WIN_HEIGHT_MAX, 1, 0);
  862. mutex_lock(&s5k6aa->lock);
  863. fi = &s5k6aa_intervals[fie->index];
  864. if (fie->width > fi->size.width || fie->height > fi->size.height)
  865. ret = -EINVAL;
  866. else
  867. fie->interval = fi->interval;
  868. mutex_unlock(&s5k6aa->lock);
  869. return ret;
  870. }
  871. static int s5k6aa_enum_mbus_code(struct v4l2_subdev *sd,
  872. struct v4l2_subdev_pad_config *cfg,
  873. struct v4l2_subdev_mbus_code_enum *code)
  874. {
  875. if (code->index >= ARRAY_SIZE(s5k6aa_formats))
  876. return -EINVAL;
  877. code->code = s5k6aa_formats[code->index].code;
  878. return 0;
  879. }
  880. static int s5k6aa_enum_frame_size(struct v4l2_subdev *sd,
  881. struct v4l2_subdev_pad_config *cfg,
  882. struct v4l2_subdev_frame_size_enum *fse)
  883. {
  884. int i = ARRAY_SIZE(s5k6aa_formats);
  885. if (fse->index > 0)
  886. return -EINVAL;
  887. while (--i)
  888. if (fse->code == s5k6aa_formats[i].code)
  889. break;
  890. fse->code = s5k6aa_formats[i].code;
  891. fse->min_width = S5K6AA_WIN_WIDTH_MIN;
  892. fse->max_width = S5K6AA_WIN_WIDTH_MAX;
  893. fse->max_height = S5K6AA_WIN_HEIGHT_MIN;
  894. fse->min_height = S5K6AA_WIN_HEIGHT_MAX;
  895. return 0;
  896. }
  897. static struct v4l2_rect *
  898. __s5k6aa_get_crop_rect(struct s5k6aa *s5k6aa, struct v4l2_subdev_pad_config *cfg,
  899. enum v4l2_subdev_format_whence which)
  900. {
  901. if (which == V4L2_SUBDEV_FORMAT_ACTIVE)
  902. return &s5k6aa->ccd_rect;
  903. WARN_ON(which != V4L2_SUBDEV_FORMAT_TRY);
  904. return v4l2_subdev_get_try_crop(&s5k6aa->sd, cfg, 0);
  905. }
  906. static void s5k6aa_try_format(struct s5k6aa *s5k6aa,
  907. struct v4l2_mbus_framefmt *mf)
  908. {
  909. unsigned int index;
  910. v4l_bound_align_image(&mf->width, S5K6AA_WIN_WIDTH_MIN,
  911. S5K6AA_WIN_WIDTH_MAX, 1,
  912. &mf->height, S5K6AA_WIN_HEIGHT_MIN,
  913. S5K6AA_WIN_HEIGHT_MAX, 1, 0);
  914. if (mf->colorspace != V4L2_COLORSPACE_JPEG &&
  915. mf->colorspace != V4L2_COLORSPACE_REC709)
  916. mf->colorspace = V4L2_COLORSPACE_JPEG;
  917. index = s5k6aa_get_pixfmt_index(s5k6aa, mf);
  918. mf->colorspace = s5k6aa_formats[index].colorspace;
  919. mf->code = s5k6aa_formats[index].code;
  920. mf->field = V4L2_FIELD_NONE;
  921. }
  922. static int s5k6aa_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
  923. struct v4l2_subdev_format *fmt)
  924. {
  925. struct s5k6aa *s5k6aa = to_s5k6aa(sd);
  926. struct v4l2_mbus_framefmt *mf;
  927. memset(fmt->reserved, 0, sizeof(fmt->reserved));
  928. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  929. mf = v4l2_subdev_get_try_format(sd, cfg, 0);
  930. fmt->format = *mf;
  931. return 0;
  932. }
  933. mutex_lock(&s5k6aa->lock);
  934. fmt->format = s5k6aa->preset->mbus_fmt;
  935. mutex_unlock(&s5k6aa->lock);
  936. return 0;
  937. }
  938. static int s5k6aa_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
  939. struct v4l2_subdev_format *fmt)
  940. {
  941. struct s5k6aa *s5k6aa = to_s5k6aa(sd);
  942. struct s5k6aa_preset *preset = s5k6aa->preset;
  943. struct v4l2_mbus_framefmt *mf;
  944. struct v4l2_rect *crop;
  945. int ret = 0;
  946. mutex_lock(&s5k6aa->lock);
  947. s5k6aa_try_format(s5k6aa, &fmt->format);
  948. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  949. mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
  950. crop = v4l2_subdev_get_try_crop(sd, cfg, 0);
  951. } else {
  952. if (s5k6aa->streaming) {
  953. ret = -EBUSY;
  954. } else {
  955. mf = &preset->mbus_fmt;
  956. crop = &s5k6aa->ccd_rect;
  957. s5k6aa->apply_cfg = 1;
  958. }
  959. }
  960. if (ret == 0) {
  961. struct v4l2_subdev_frame_interval fiv = {
  962. .interval = {0, 1}
  963. };
  964. *mf = fmt->format;
  965. /*
  966. * Make sure the crop window is valid, i.e. its size is
  967. * greater than the output window, as the ISP supports
  968. * only down-scaling.
  969. */
  970. crop->width = clamp_t(unsigned int, crop->width, mf->width,
  971. S5K6AA_WIN_WIDTH_MAX);
  972. crop->height = clamp_t(unsigned int, crop->height, mf->height,
  973. S5K6AA_WIN_HEIGHT_MAX);
  974. crop->left = clamp_t(unsigned int, crop->left, 0,
  975. S5K6AA_WIN_WIDTH_MAX - crop->width);
  976. crop->top = clamp_t(unsigned int, crop->top, 0,
  977. S5K6AA_WIN_HEIGHT_MAX - crop->height);
  978. /* Reset to minimum possible frame interval */
  979. ret = __s5k6aa_set_frame_interval(s5k6aa, &fiv);
  980. }
  981. mutex_unlock(&s5k6aa->lock);
  982. return ret;
  983. }
  984. static int s5k6aa_get_selection(struct v4l2_subdev *sd,
  985. struct v4l2_subdev_pad_config *cfg,
  986. struct v4l2_subdev_selection *sel)
  987. {
  988. struct s5k6aa *s5k6aa = to_s5k6aa(sd);
  989. struct v4l2_rect *rect;
  990. if (sel->target != V4L2_SEL_TGT_CROP)
  991. return -EINVAL;
  992. memset(sel->reserved, 0, sizeof(sel->reserved));
  993. mutex_lock(&s5k6aa->lock);
  994. rect = __s5k6aa_get_crop_rect(s5k6aa, cfg, sel->which);
  995. sel->r = *rect;
  996. mutex_unlock(&s5k6aa->lock);
  997. v4l2_dbg(1, debug, sd, "Current crop rectangle: (%d,%d)/%dx%d\n",
  998. rect->left, rect->top, rect->width, rect->height);
  999. return 0;
  1000. }
  1001. static int s5k6aa_set_selection(struct v4l2_subdev *sd,
  1002. struct v4l2_subdev_pad_config *cfg,
  1003. struct v4l2_subdev_selection *sel)
  1004. {
  1005. struct s5k6aa *s5k6aa = to_s5k6aa(sd);
  1006. struct v4l2_mbus_framefmt *mf;
  1007. unsigned int max_x, max_y;
  1008. struct v4l2_rect *crop_r;
  1009. if (sel->target != V4L2_SEL_TGT_CROP)
  1010. return -EINVAL;
  1011. mutex_lock(&s5k6aa->lock);
  1012. crop_r = __s5k6aa_get_crop_rect(s5k6aa, cfg, sel->which);
  1013. if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  1014. mf = &s5k6aa->preset->mbus_fmt;
  1015. s5k6aa->apply_crop = 1;
  1016. } else {
  1017. mf = v4l2_subdev_get_try_format(sd, cfg, 0);
  1018. }
  1019. v4l_bound_align_image(&sel->r.width, mf->width,
  1020. S5K6AA_WIN_WIDTH_MAX, 1,
  1021. &sel->r.height, mf->height,
  1022. S5K6AA_WIN_HEIGHT_MAX, 1, 0);
  1023. max_x = (S5K6AA_WIN_WIDTH_MAX - sel->r.width) & ~1;
  1024. max_y = (S5K6AA_WIN_HEIGHT_MAX - sel->r.height) & ~1;
  1025. sel->r.left = clamp_t(unsigned int, sel->r.left, 0, max_x);
  1026. sel->r.top = clamp_t(unsigned int, sel->r.top, 0, max_y);
  1027. *crop_r = sel->r;
  1028. mutex_unlock(&s5k6aa->lock);
  1029. v4l2_dbg(1, debug, sd, "Set crop rectangle: (%d,%d)/%dx%d\n",
  1030. crop_r->left, crop_r->top, crop_r->width, crop_r->height);
  1031. return 0;
  1032. }
  1033. static const struct v4l2_subdev_pad_ops s5k6aa_pad_ops = {
  1034. .enum_mbus_code = s5k6aa_enum_mbus_code,
  1035. .enum_frame_size = s5k6aa_enum_frame_size,
  1036. .enum_frame_interval = s5k6aa_enum_frame_interval,
  1037. .get_fmt = s5k6aa_get_fmt,
  1038. .set_fmt = s5k6aa_set_fmt,
  1039. .get_selection = s5k6aa_get_selection,
  1040. .set_selection = s5k6aa_set_selection,
  1041. };
  1042. static const struct v4l2_subdev_video_ops s5k6aa_video_ops = {
  1043. .g_frame_interval = s5k6aa_g_frame_interval,
  1044. .s_frame_interval = s5k6aa_s_frame_interval,
  1045. .s_stream = s5k6aa_s_stream,
  1046. };
  1047. /*
  1048. * V4L2 subdev controls
  1049. */
  1050. static int s5k6aa_s_ctrl(struct v4l2_ctrl *ctrl)
  1051. {
  1052. struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
  1053. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1054. struct s5k6aa *s5k6aa = to_s5k6aa(sd);
  1055. int idx, err = 0;
  1056. v4l2_dbg(1, debug, sd, "ctrl: 0x%x, value: %d\n", ctrl->id, ctrl->val);
  1057. mutex_lock(&s5k6aa->lock);
  1058. /*
  1059. * If the device is not powered up by the host driver do
  1060. * not apply any controls to H/W at this time. Instead
  1061. * the controls will be restored right after power-up.
  1062. */
  1063. if (s5k6aa->power == 0)
  1064. goto unlock;
  1065. idx = s5k6aa->preset->index;
  1066. switch (ctrl->id) {
  1067. case V4L2_CID_AUTO_WHITE_BALANCE:
  1068. err = s5k6aa_set_awb(s5k6aa, ctrl->val);
  1069. break;
  1070. case V4L2_CID_BRIGHTNESS:
  1071. err = s5k6aa_write(client, REG_USER_BRIGHTNESS, ctrl->val);
  1072. break;
  1073. case V4L2_CID_COLORFX:
  1074. err = s5k6aa_set_colorfx(s5k6aa, ctrl->val);
  1075. break;
  1076. case V4L2_CID_CONTRAST:
  1077. err = s5k6aa_write(client, REG_USER_CONTRAST, ctrl->val);
  1078. break;
  1079. case V4L2_CID_EXPOSURE_AUTO:
  1080. err = s5k6aa_set_auto_exposure(s5k6aa, ctrl->val);
  1081. break;
  1082. case V4L2_CID_HFLIP:
  1083. err = s5k6aa_set_mirror(s5k6aa, ctrl->val);
  1084. if (err)
  1085. break;
  1086. err = s5k6aa_write(client, REG_G_PREV_CFG_CHG, 1);
  1087. break;
  1088. case V4L2_CID_POWER_LINE_FREQUENCY:
  1089. err = s5k6aa_set_anti_flicker(s5k6aa, ctrl->val);
  1090. break;
  1091. case V4L2_CID_SATURATION:
  1092. err = s5k6aa_write(client, REG_USER_SATURATION, ctrl->val);
  1093. break;
  1094. case V4L2_CID_SHARPNESS:
  1095. err = s5k6aa_write(client, REG_USER_SHARPBLUR, ctrl->val);
  1096. break;
  1097. case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
  1098. err = s5k6aa_write(client, REG_P_COLORTEMP(idx), ctrl->val);
  1099. if (err)
  1100. break;
  1101. err = s5k6aa_write(client, REG_G_PREV_CFG_CHG, 1);
  1102. break;
  1103. }
  1104. unlock:
  1105. mutex_unlock(&s5k6aa->lock);
  1106. return err;
  1107. }
  1108. static const struct v4l2_ctrl_ops s5k6aa_ctrl_ops = {
  1109. .s_ctrl = s5k6aa_s_ctrl,
  1110. };
  1111. static int s5k6aa_log_status(struct v4l2_subdev *sd)
  1112. {
  1113. v4l2_ctrl_handler_log_status(sd->ctrl_handler, sd->name);
  1114. return 0;
  1115. }
  1116. #define V4L2_CID_RED_GAIN (V4L2_CTRL_CLASS_CAMERA | 0x1001)
  1117. #define V4L2_CID_GREEN_GAIN (V4L2_CTRL_CLASS_CAMERA | 0x1002)
  1118. #define V4L2_CID_BLUE_GAIN (V4L2_CTRL_CLASS_CAMERA | 0x1003)
  1119. static const struct v4l2_ctrl_config s5k6aa_ctrls[] = {
  1120. {
  1121. .ops = &s5k6aa_ctrl_ops,
  1122. .id = V4L2_CID_RED_GAIN,
  1123. .type = V4L2_CTRL_TYPE_INTEGER,
  1124. .name = "Gain, Red",
  1125. .min = 0,
  1126. .max = 256,
  1127. .def = 127,
  1128. .step = 1,
  1129. }, {
  1130. .ops = &s5k6aa_ctrl_ops,
  1131. .id = V4L2_CID_GREEN_GAIN,
  1132. .type = V4L2_CTRL_TYPE_INTEGER,
  1133. .name = "Gain, Green",
  1134. .min = 0,
  1135. .max = 256,
  1136. .def = 127,
  1137. .step = 1,
  1138. }, {
  1139. .ops = &s5k6aa_ctrl_ops,
  1140. .id = V4L2_CID_BLUE_GAIN,
  1141. .type = V4L2_CTRL_TYPE_INTEGER,
  1142. .name = "Gain, Blue",
  1143. .min = 0,
  1144. .max = 256,
  1145. .def = 127,
  1146. .step = 1,
  1147. },
  1148. };
  1149. static int s5k6aa_initialize_ctrls(struct s5k6aa *s5k6aa)
  1150. {
  1151. const struct v4l2_ctrl_ops *ops = &s5k6aa_ctrl_ops;
  1152. struct s5k6aa_ctrls *ctrls = &s5k6aa->ctrls;
  1153. struct v4l2_ctrl_handler *hdl = &ctrls->handler;
  1154. int ret = v4l2_ctrl_handler_init(hdl, 16);
  1155. if (ret)
  1156. return ret;
  1157. /* Auto white balance cluster */
  1158. ctrls->awb = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTO_WHITE_BALANCE,
  1159. 0, 1, 1, 1);
  1160. ctrls->gain_red = v4l2_ctrl_new_custom(hdl, &s5k6aa_ctrls[0], NULL);
  1161. ctrls->gain_green = v4l2_ctrl_new_custom(hdl, &s5k6aa_ctrls[1], NULL);
  1162. ctrls->gain_blue = v4l2_ctrl_new_custom(hdl, &s5k6aa_ctrls[2], NULL);
  1163. v4l2_ctrl_auto_cluster(4, &ctrls->awb, 0, false);
  1164. ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  1165. ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  1166. v4l2_ctrl_cluster(2, &ctrls->hflip);
  1167. ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
  1168. V4L2_CID_EXPOSURE_AUTO,
  1169. V4L2_EXPOSURE_MANUAL, 0, V4L2_EXPOSURE_AUTO);
  1170. /* Exposure time: x 1 us */
  1171. ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
  1172. 0, 6000000U, 1, 100000U);
  1173. /* Total gain: 256 <=> 1x */
  1174. ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
  1175. 0, 256, 1, 256);
  1176. v4l2_ctrl_auto_cluster(3, &ctrls->auto_exp, 0, false);
  1177. v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_POWER_LINE_FREQUENCY,
  1178. V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
  1179. V4L2_CID_POWER_LINE_FREQUENCY_AUTO);
  1180. v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_COLORFX,
  1181. V4L2_COLORFX_SKY_BLUE, ~0x6f, V4L2_COLORFX_NONE);
  1182. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_WHITE_BALANCE_TEMPERATURE,
  1183. 0, 256, 1, 0);
  1184. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, -127, 127, 1, 0);
  1185. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS, -127, 127, 1, 0);
  1186. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, -127, 127, 1, 0);
  1187. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SHARPNESS, -127, 127, 1, 0);
  1188. if (hdl->error) {
  1189. ret = hdl->error;
  1190. v4l2_ctrl_handler_free(hdl);
  1191. return ret;
  1192. }
  1193. s5k6aa->sd.ctrl_handler = hdl;
  1194. return 0;
  1195. }
  1196. /*
  1197. * V4L2 subdev internal operations
  1198. */
  1199. static int s5k6aa_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  1200. {
  1201. struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(sd, fh->pad, 0);
  1202. struct v4l2_rect *crop = v4l2_subdev_get_try_crop(sd, fh->pad, 0);
  1203. format->colorspace = s5k6aa_formats[0].colorspace;
  1204. format->code = s5k6aa_formats[0].code;
  1205. format->width = S5K6AA_OUT_WIDTH_DEF;
  1206. format->height = S5K6AA_OUT_HEIGHT_DEF;
  1207. format->field = V4L2_FIELD_NONE;
  1208. crop->width = S5K6AA_WIN_WIDTH_MAX;
  1209. crop->height = S5K6AA_WIN_HEIGHT_MAX;
  1210. crop->left = 0;
  1211. crop->top = 0;
  1212. return 0;
  1213. }
  1214. static int s5k6aa_check_fw_revision(struct s5k6aa *s5k6aa)
  1215. {
  1216. struct i2c_client *client = v4l2_get_subdevdata(&s5k6aa->sd);
  1217. u16 api_ver = 0, fw_rev = 0;
  1218. int ret = s5k6aa_set_ahb_address(client);
  1219. if (!ret)
  1220. ret = s5k6aa_read(client, REG_FW_APIVER, &api_ver);
  1221. if (!ret)
  1222. ret = s5k6aa_read(client, REG_FW_REVISION, &fw_rev);
  1223. if (ret) {
  1224. v4l2_err(&s5k6aa->sd, "FW revision check failed!\n");
  1225. return ret;
  1226. }
  1227. v4l2_info(&s5k6aa->sd, "FW API ver.: 0x%X, FW rev.: 0x%X\n",
  1228. api_ver, fw_rev);
  1229. return api_ver == S5K6AAFX_FW_APIVER ? 0 : -ENODEV;
  1230. }
  1231. static int s5k6aa_registered(struct v4l2_subdev *sd)
  1232. {
  1233. struct s5k6aa *s5k6aa = to_s5k6aa(sd);
  1234. int ret;
  1235. mutex_lock(&s5k6aa->lock);
  1236. ret = __s5k6aa_power_on(s5k6aa);
  1237. if (!ret) {
  1238. msleep(100);
  1239. ret = s5k6aa_check_fw_revision(s5k6aa);
  1240. __s5k6aa_power_off(s5k6aa);
  1241. }
  1242. mutex_unlock(&s5k6aa->lock);
  1243. return ret;
  1244. }
  1245. static const struct v4l2_subdev_internal_ops s5k6aa_subdev_internal_ops = {
  1246. .registered = s5k6aa_registered,
  1247. .open = s5k6aa_open,
  1248. };
  1249. static const struct v4l2_subdev_core_ops s5k6aa_core_ops = {
  1250. .s_power = s5k6aa_set_power,
  1251. .log_status = s5k6aa_log_status,
  1252. };
  1253. static const struct v4l2_subdev_ops s5k6aa_subdev_ops = {
  1254. .core = &s5k6aa_core_ops,
  1255. .pad = &s5k6aa_pad_ops,
  1256. .video = &s5k6aa_video_ops,
  1257. };
  1258. /*
  1259. * GPIO setup
  1260. */
  1261. static int s5k6aa_configure_gpios(struct s5k6aa *s5k6aa,
  1262. const struct s5k6aa_platform_data *pdata)
  1263. {
  1264. struct i2c_client *client = v4l2_get_subdevdata(&s5k6aa->sd);
  1265. const struct s5k6aa_gpio *gpio;
  1266. unsigned long flags;
  1267. int ret;
  1268. s5k6aa->gpio[STBY].gpio = -EINVAL;
  1269. s5k6aa->gpio[RST].gpio = -EINVAL;
  1270. gpio = &pdata->gpio_stby;
  1271. if (gpio_is_valid(gpio->gpio)) {
  1272. flags = (gpio->level ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW)
  1273. | GPIOF_EXPORT;
  1274. ret = devm_gpio_request_one(&client->dev, gpio->gpio, flags,
  1275. "S5K6AA_STBY");
  1276. if (ret < 0)
  1277. return ret;
  1278. s5k6aa->gpio[STBY] = *gpio;
  1279. }
  1280. gpio = &pdata->gpio_reset;
  1281. if (gpio_is_valid(gpio->gpio)) {
  1282. flags = (gpio->level ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW)
  1283. | GPIOF_EXPORT;
  1284. ret = devm_gpio_request_one(&client->dev, gpio->gpio, flags,
  1285. "S5K6AA_RST");
  1286. if (ret < 0)
  1287. return ret;
  1288. s5k6aa->gpio[RST] = *gpio;
  1289. }
  1290. return 0;
  1291. }
  1292. static int s5k6aa_probe(struct i2c_client *client,
  1293. const struct i2c_device_id *id)
  1294. {
  1295. const struct s5k6aa_platform_data *pdata = client->dev.platform_data;
  1296. struct v4l2_subdev *sd;
  1297. struct s5k6aa *s5k6aa;
  1298. int i, ret;
  1299. if (pdata == NULL) {
  1300. dev_err(&client->dev, "Platform data not specified\n");
  1301. return -EINVAL;
  1302. }
  1303. if (pdata->mclk_frequency == 0) {
  1304. dev_err(&client->dev, "MCLK frequency not specified\n");
  1305. return -EINVAL;
  1306. }
  1307. s5k6aa = devm_kzalloc(&client->dev, sizeof(*s5k6aa), GFP_KERNEL);
  1308. if (!s5k6aa)
  1309. return -ENOMEM;
  1310. mutex_init(&s5k6aa->lock);
  1311. s5k6aa->mclk_frequency = pdata->mclk_frequency;
  1312. s5k6aa->bus_type = pdata->bus_type;
  1313. s5k6aa->mipi_lanes = pdata->nlanes;
  1314. s5k6aa->s_power = pdata->set_power;
  1315. s5k6aa->inv_hflip = pdata->horiz_flip;
  1316. s5k6aa->inv_vflip = pdata->vert_flip;
  1317. sd = &s5k6aa->sd;
  1318. v4l2_i2c_subdev_init(sd, client, &s5k6aa_subdev_ops);
  1319. strlcpy(sd->name, DRIVER_NAME, sizeof(sd->name));
  1320. sd->internal_ops = &s5k6aa_subdev_internal_ops;
  1321. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1322. s5k6aa->pad.flags = MEDIA_PAD_FL_SOURCE;
  1323. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1324. ret = media_entity_pads_init(&sd->entity, 1, &s5k6aa->pad);
  1325. if (ret)
  1326. return ret;
  1327. ret = s5k6aa_configure_gpios(s5k6aa, pdata);
  1328. if (ret)
  1329. goto out_err;
  1330. for (i = 0; i < S5K6AA_NUM_SUPPLIES; i++)
  1331. s5k6aa->supplies[i].supply = s5k6aa_supply_names[i];
  1332. ret = devm_regulator_bulk_get(&client->dev, S5K6AA_NUM_SUPPLIES,
  1333. s5k6aa->supplies);
  1334. if (ret) {
  1335. dev_err(&client->dev, "Failed to get regulators\n");
  1336. goto out_err;
  1337. }
  1338. ret = s5k6aa_initialize_ctrls(s5k6aa);
  1339. if (ret)
  1340. goto out_err;
  1341. s5k6aa_presets_data_init(s5k6aa);
  1342. s5k6aa->ccd_rect.width = S5K6AA_WIN_WIDTH_MAX;
  1343. s5k6aa->ccd_rect.height = S5K6AA_WIN_HEIGHT_MAX;
  1344. s5k6aa->ccd_rect.left = 0;
  1345. s5k6aa->ccd_rect.top = 0;
  1346. return 0;
  1347. out_err:
  1348. media_entity_cleanup(&s5k6aa->sd.entity);
  1349. return ret;
  1350. }
  1351. static int s5k6aa_remove(struct i2c_client *client)
  1352. {
  1353. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1354. v4l2_device_unregister_subdev(sd);
  1355. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1356. media_entity_cleanup(&sd->entity);
  1357. return 0;
  1358. }
  1359. static const struct i2c_device_id s5k6aa_id[] = {
  1360. { DRIVER_NAME, 0 },
  1361. { },
  1362. };
  1363. MODULE_DEVICE_TABLE(i2c, s5k6aa_id);
  1364. static struct i2c_driver s5k6aa_i2c_driver = {
  1365. .driver = {
  1366. .name = DRIVER_NAME
  1367. },
  1368. .probe = s5k6aa_probe,
  1369. .remove = s5k6aa_remove,
  1370. .id_table = s5k6aa_id,
  1371. };
  1372. module_i2c_driver(s5k6aa_i2c_driver);
  1373. MODULE_DESCRIPTION("Samsung S5K6AA(FX) SXGA camera driver");
  1374. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  1375. MODULE_LICENSE("GPL");