s5k6aa.c 43 KB

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