s5k6aa.c 44 KB

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