s5k6aa.c 44 KB

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