s5k6aa.c 44 KB

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