sr030pc30.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /*
  2. * Driver for SiliconFile SR030PC30 VGA (1/10-Inch) Image Sensor with ISP
  3. *
  4. * Copyright (C) 2010 Samsung Electronics Co., Ltd
  5. * Author: Sylwester Nawrocki, s.nawrocki@samsung.com
  6. *
  7. * Based on original driver authored by Dongsoo Nathaniel Kim
  8. * and HeungJun Kim <riverful.kim@samsung.com>.
  9. *
  10. * Based on mt9v011 Micron Digital Image Sensor driver
  11. * Copyright (c) 2009 Mauro Carvalho Chehab (mchehab@redhat.com)
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. */
  18. #include <linux/i2c.h>
  19. #include <linux/delay.h>
  20. #include <media/v4l2-device.h>
  21. #include <media/v4l2-subdev.h>
  22. #include <media/v4l2-mediabus.h>
  23. #include <media/sr030pc30.h>
  24. static int debug;
  25. module_param(debug, int, 0644);
  26. #define MODULE_NAME "SR030PC30"
  27. /*
  28. * Register offsets within a page
  29. * b15..b8 - page id, b7..b0 - register address
  30. */
  31. #define POWER_CTRL_REG 0x0001
  32. #define PAGEMODE_REG 0x03
  33. #define DEVICE_ID_REG 0x0004
  34. #define NOON010PC30_ID 0x86
  35. #define SR030PC30_ID 0x8C
  36. #define VDO_CTL1_REG 0x0010
  37. #define SUBSAMPL_NONE_VGA 0
  38. #define SUBSAMPL_QVGA 0x10
  39. #define SUBSAMPL_QQVGA 0x20
  40. #define VDO_CTL2_REG 0x0011
  41. #define SYNC_CTL_REG 0x0012
  42. #define WIN_ROWH_REG 0x0020
  43. #define WIN_ROWL_REG 0x0021
  44. #define WIN_COLH_REG 0x0022
  45. #define WIN_COLL_REG 0x0023
  46. #define WIN_HEIGHTH_REG 0x0024
  47. #define WIN_HEIGHTL_REG 0x0025
  48. #define WIN_WIDTHH_REG 0x0026
  49. #define WIN_WIDTHL_REG 0x0027
  50. #define HBLANKH_REG 0x0040
  51. #define HBLANKL_REG 0x0041
  52. #define VSYNCH_REG 0x0042
  53. #define VSYNCL_REG 0x0043
  54. /* page 10 */
  55. #define ISP_CTL_REG(n) (0x1010 + (n))
  56. #define YOFS_REG 0x1040
  57. #define DARK_YOFS_REG 0x1041
  58. #define AG_ABRTH_REG 0x1050
  59. #define SAT_CTL_REG 0x1060
  60. #define BSAT_REG 0x1061
  61. #define RSAT_REG 0x1062
  62. #define AG_SAT_TH_REG 0x1063
  63. /* page 11 */
  64. #define ZLPF_CTRL_REG 0x1110
  65. #define ZLPF_CTRL2_REG 0x1112
  66. #define ZLPF_AGH_THR_REG 0x1121
  67. #define ZLPF_THR_REG 0x1160
  68. #define ZLPF_DYN_THR_REG 0x1160
  69. /* page 12 */
  70. #define YCLPF_CTL1_REG 0x1240
  71. #define YCLPF_CTL2_REG 0x1241
  72. #define YCLPF_THR_REG 0x1250
  73. #define BLPF_CTL_REG 0x1270
  74. #define BLPF_THR1_REG 0x1274
  75. #define BLPF_THR2_REG 0x1275
  76. /* page 14 - Lens Shading Compensation */
  77. #define LENS_CTRL_REG 0x1410
  78. #define LENS_XCEN_REG 0x1420
  79. #define LENS_YCEN_REG 0x1421
  80. #define LENS_R_COMP_REG 0x1422
  81. #define LENS_G_COMP_REG 0x1423
  82. #define LENS_B_COMP_REG 0x1424
  83. /* page 15 - Color correction */
  84. #define CMC_CTL_REG 0x1510
  85. #define CMC_OFSGH_REG 0x1514
  86. #define CMC_OFSGL_REG 0x1516
  87. #define CMC_SIGN_REG 0x1517
  88. /* Color correction coefficients */
  89. #define CMC_COEF_REG(n) (0x1530 + (n))
  90. /* Color correction offset coefficients */
  91. #define CMC_OFS_REG(n) (0x1540 + (n))
  92. /* page 16 - Gamma correction */
  93. #define GMA_CTL_REG 0x1610
  94. /* Gamma correction coefficients 0.14 */
  95. #define GMA_COEF_REG(n) (0x1630 + (n))
  96. /* page 20 - Auto Exposure */
  97. #define AE_CTL1_REG 0x2010
  98. #define AE_CTL2_REG 0x2011
  99. #define AE_FRM_CTL_REG 0x2020
  100. #define AE_FINE_CTL_REG(n) (0x2028 + (n))
  101. #define EXP_TIMEH_REG 0x2083
  102. #define EXP_TIMEM_REG 0x2084
  103. #define EXP_TIMEL_REG 0x2085
  104. #define EXP_MMINH_REG 0x2086
  105. #define EXP_MMINL_REG 0x2087
  106. #define EXP_MMAXH_REG 0x2088
  107. #define EXP_MMAXM_REG 0x2089
  108. #define EXP_MMAXL_REG 0x208A
  109. /* page 22 - Auto White Balance */
  110. #define AWB_CTL1_REG 0x2210
  111. #define AWB_ENABLE 0x80
  112. #define AWB_CTL2_REG 0x2211
  113. #define MWB_ENABLE 0x01
  114. /* RGB gain control (manual WB) when AWB_CTL1[7]=0 */
  115. #define AWB_RGAIN_REG 0x2280
  116. #define AWB_GGAIN_REG 0x2281
  117. #define AWB_BGAIN_REG 0x2282
  118. #define AWB_RMAX_REG 0x2283
  119. #define AWB_RMIN_REG 0x2284
  120. #define AWB_BMAX_REG 0x2285
  121. #define AWB_BMIN_REG 0x2286
  122. /* R, B gain range in bright light conditions */
  123. #define AWB_RMAXB_REG 0x2287
  124. #define AWB_RMINB_REG 0x2288
  125. #define AWB_BMAXB_REG 0x2289
  126. #define AWB_BMINB_REG 0x228A
  127. /* manual white balance, when AWB_CTL2[0]=1 */
  128. #define MWB_RGAIN_REG 0x22B2
  129. #define MWB_BGAIN_REG 0x22B3
  130. /* the token to mark an array end */
  131. #define REG_TERM 0xFFFF
  132. /* Minimum and maximum exposure time in ms */
  133. #define EXPOS_MIN_MS 1
  134. #define EXPOS_MAX_MS 125
  135. struct sr030pc30_info {
  136. struct v4l2_subdev sd;
  137. const struct sr030pc30_platform_data *pdata;
  138. const struct sr030pc30_format *curr_fmt;
  139. const struct sr030pc30_frmsize *curr_win;
  140. unsigned int auto_wb:1;
  141. unsigned int auto_exp:1;
  142. unsigned int hflip:1;
  143. unsigned int vflip:1;
  144. unsigned int sleep:1;
  145. unsigned int exposure;
  146. u8 blue_balance;
  147. u8 red_balance;
  148. u8 i2c_reg_page;
  149. };
  150. struct sr030pc30_format {
  151. enum v4l2_mbus_pixelcode code;
  152. enum v4l2_colorspace colorspace;
  153. u16 ispctl1_reg;
  154. };
  155. struct sr030pc30_frmsize {
  156. u16 width;
  157. u16 height;
  158. int vid_ctl1;
  159. };
  160. struct i2c_regval {
  161. u16 addr;
  162. u16 val;
  163. };
  164. static const struct v4l2_queryctrl sr030pc30_ctrl[] = {
  165. {
  166. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  167. .type = V4L2_CTRL_TYPE_BOOLEAN,
  168. .name = "Auto White Balance",
  169. .minimum = 0,
  170. .maximum = 1,
  171. .step = 1,
  172. .default_value = 1,
  173. }, {
  174. .id = V4L2_CID_RED_BALANCE,
  175. .type = V4L2_CTRL_TYPE_INTEGER,
  176. .name = "Red Balance",
  177. .minimum = 0,
  178. .maximum = 127,
  179. .step = 1,
  180. .default_value = 64,
  181. .flags = 0,
  182. }, {
  183. .id = V4L2_CID_BLUE_BALANCE,
  184. .type = V4L2_CTRL_TYPE_INTEGER,
  185. .name = "Blue Balance",
  186. .minimum = 0,
  187. .maximum = 127,
  188. .step = 1,
  189. .default_value = 64,
  190. }, {
  191. .id = V4L2_CID_EXPOSURE_AUTO,
  192. .type = V4L2_CTRL_TYPE_INTEGER,
  193. .name = "Auto Exposure",
  194. .minimum = 0,
  195. .maximum = 1,
  196. .step = 1,
  197. .default_value = 1,
  198. }, {
  199. .id = V4L2_CID_EXPOSURE,
  200. .type = V4L2_CTRL_TYPE_INTEGER,
  201. .name = "Exposure",
  202. .minimum = EXPOS_MIN_MS,
  203. .maximum = EXPOS_MAX_MS,
  204. .step = 1,
  205. .default_value = 1,
  206. }, {
  207. }
  208. };
  209. /* supported resolutions */
  210. static const struct sr030pc30_frmsize sr030pc30_sizes[] = {
  211. {
  212. .width = 640,
  213. .height = 480,
  214. .vid_ctl1 = SUBSAMPL_NONE_VGA,
  215. }, {
  216. .width = 320,
  217. .height = 240,
  218. .vid_ctl1 = SUBSAMPL_QVGA,
  219. }, {
  220. .width = 160,
  221. .height = 120,
  222. .vid_ctl1 = SUBSAMPL_QQVGA,
  223. },
  224. };
  225. /* supported pixel formats */
  226. static const struct sr030pc30_format sr030pc30_formats[] = {
  227. {
  228. .code = V4L2_MBUS_FMT_YUYV8_2X8,
  229. .colorspace = V4L2_COLORSPACE_JPEG,
  230. .ispctl1_reg = 0x03,
  231. }, {
  232. .code = V4L2_MBUS_FMT_YVYU8_2X8,
  233. .colorspace = V4L2_COLORSPACE_JPEG,
  234. .ispctl1_reg = 0x02,
  235. }, {
  236. .code = V4L2_MBUS_FMT_VYUY8_2X8,
  237. .colorspace = V4L2_COLORSPACE_JPEG,
  238. .ispctl1_reg = 0,
  239. }, {
  240. .code = V4L2_MBUS_FMT_UYVY8_2X8,
  241. .colorspace = V4L2_COLORSPACE_JPEG,
  242. .ispctl1_reg = 0x01,
  243. }, {
  244. .code = V4L2_MBUS_FMT_RGB565_2X8_BE,
  245. .colorspace = V4L2_COLORSPACE_JPEG,
  246. .ispctl1_reg = 0x40,
  247. },
  248. };
  249. static const struct i2c_regval sr030pc30_base_regs[] = {
  250. /* Window size and position within pixel matrix */
  251. { WIN_ROWH_REG, 0x00 }, { WIN_ROWL_REG, 0x06 },
  252. { WIN_COLH_REG, 0x00 }, { WIN_COLL_REG, 0x06 },
  253. { WIN_HEIGHTH_REG, 0x01 }, { WIN_HEIGHTL_REG, 0xE0 },
  254. { WIN_WIDTHH_REG, 0x02 }, { WIN_WIDTHL_REG, 0x80 },
  255. { HBLANKH_REG, 0x01 }, { HBLANKL_REG, 0x50 },
  256. { VSYNCH_REG, 0x00 }, { VSYNCL_REG, 0x14 },
  257. { SYNC_CTL_REG, 0 },
  258. /* Color corection and saturation */
  259. { ISP_CTL_REG(0), 0x30 }, { YOFS_REG, 0x80 },
  260. { DARK_YOFS_REG, 0x04 }, { AG_ABRTH_REG, 0x78 },
  261. { SAT_CTL_REG, 0x1F }, { BSAT_REG, 0x90 },
  262. { AG_SAT_TH_REG, 0xF0 }, { 0x1064, 0x80 },
  263. { CMC_CTL_REG, 0x03 }, { CMC_OFSGH_REG, 0x3C },
  264. { CMC_OFSGL_REG, 0x2C }, { CMC_SIGN_REG, 0x2F },
  265. { CMC_COEF_REG(0), 0xCB }, { CMC_OFS_REG(0), 0x87 },
  266. { CMC_COEF_REG(1), 0x61 }, { CMC_OFS_REG(1), 0x18 },
  267. { CMC_COEF_REG(2), 0x16 }, { CMC_OFS_REG(2), 0x91 },
  268. { CMC_COEF_REG(3), 0x23 }, { CMC_OFS_REG(3), 0x94 },
  269. { CMC_COEF_REG(4), 0xCE }, { CMC_OFS_REG(4), 0x9f },
  270. { CMC_COEF_REG(5), 0x2B }, { CMC_OFS_REG(5), 0x33 },
  271. { CMC_COEF_REG(6), 0x01 }, { CMC_OFS_REG(6), 0x00 },
  272. { CMC_COEF_REG(7), 0x34 }, { CMC_OFS_REG(7), 0x94 },
  273. { CMC_COEF_REG(8), 0x75 }, { CMC_OFS_REG(8), 0x14 },
  274. /* Color corection coefficients */
  275. { GMA_CTL_REG, 0x03 }, { GMA_COEF_REG(0), 0x00 },
  276. { GMA_COEF_REG(1), 0x19 }, { GMA_COEF_REG(2), 0x26 },
  277. { GMA_COEF_REG(3), 0x3B }, { GMA_COEF_REG(4), 0x5D },
  278. { GMA_COEF_REG(5), 0x79 }, { GMA_COEF_REG(6), 0x8E },
  279. { GMA_COEF_REG(7), 0x9F }, { GMA_COEF_REG(8), 0xAF },
  280. { GMA_COEF_REG(9), 0xBD }, { GMA_COEF_REG(10), 0xCA },
  281. { GMA_COEF_REG(11), 0xDD }, { GMA_COEF_REG(12), 0xEC },
  282. { GMA_COEF_REG(13), 0xF7 }, { GMA_COEF_REG(14), 0xFF },
  283. /* Noise reduction, Z-LPF, YC-LPF and BLPF filters setup */
  284. { ZLPF_CTRL_REG, 0x99 }, { ZLPF_CTRL2_REG, 0x0E },
  285. { ZLPF_AGH_THR_REG, 0x29 }, { ZLPF_THR_REG, 0x0F },
  286. { ZLPF_DYN_THR_REG, 0x63 }, { YCLPF_CTL1_REG, 0x23 },
  287. { YCLPF_CTL2_REG, 0x3B }, { YCLPF_THR_REG, 0x05 },
  288. { BLPF_CTL_REG, 0x1D }, { BLPF_THR1_REG, 0x05 },
  289. { BLPF_THR2_REG, 0x04 },
  290. /* Automatic white balance */
  291. { AWB_CTL1_REG, 0xFB }, { AWB_CTL2_REG, 0x26 },
  292. { AWB_RMAX_REG, 0x54 }, { AWB_RMIN_REG, 0x2B },
  293. { AWB_BMAX_REG, 0x57 }, { AWB_BMIN_REG, 0x29 },
  294. { AWB_RMAXB_REG, 0x50 }, { AWB_RMINB_REG, 0x43 },
  295. { AWB_BMAXB_REG, 0x30 }, { AWB_BMINB_REG, 0x22 },
  296. /* Auto exposure */
  297. { AE_CTL1_REG, 0x8C }, { AE_CTL2_REG, 0x04 },
  298. { AE_FRM_CTL_REG, 0x01 }, { AE_FINE_CTL_REG(0), 0x3F },
  299. { AE_FINE_CTL_REG(1), 0xA3 }, { AE_FINE_CTL_REG(3), 0x34 },
  300. /* Lens shading compensation */
  301. { LENS_CTRL_REG, 0x01 }, { LENS_XCEN_REG, 0x80 },
  302. { LENS_YCEN_REG, 0x70 }, { LENS_R_COMP_REG, 0x53 },
  303. { LENS_G_COMP_REG, 0x40 }, { LENS_B_COMP_REG, 0x3e },
  304. { REG_TERM, 0 },
  305. };
  306. static inline struct sr030pc30_info *to_sr030pc30(struct v4l2_subdev *sd)
  307. {
  308. return container_of(sd, struct sr030pc30_info, sd);
  309. }
  310. static inline int set_i2c_page(struct sr030pc30_info *info,
  311. struct i2c_client *client, unsigned int reg)
  312. {
  313. int ret;
  314. u32 page = reg >> 8 & 0xFF;
  315. if (info->i2c_reg_page != page && (reg & 0xFF) != 0x03) {
  316. ret = i2c_smbus_write_byte_data(client, PAGEMODE_REG, page);
  317. if (!ret)
  318. info->i2c_reg_page = page;
  319. }
  320. return ret;
  321. }
  322. static int cam_i2c_read(struct v4l2_subdev *sd, u32 reg_addr)
  323. {
  324. struct i2c_client *client = v4l2_get_subdevdata(sd);
  325. struct sr030pc30_info *info = to_sr030pc30(sd);
  326. int ret = set_i2c_page(info, client, reg_addr);
  327. if (!ret)
  328. ret = i2c_smbus_read_byte_data(client, reg_addr & 0xFF);
  329. return ret;
  330. }
  331. static int cam_i2c_write(struct v4l2_subdev *sd, u32 reg_addr, u32 val)
  332. {
  333. struct i2c_client *client = v4l2_get_subdevdata(sd);
  334. struct sr030pc30_info *info = to_sr030pc30(sd);
  335. int ret = set_i2c_page(info, client, reg_addr);
  336. if (!ret)
  337. ret = i2c_smbus_write_byte_data(
  338. client, reg_addr & 0xFF, val);
  339. return ret;
  340. }
  341. static inline int sr030pc30_bulk_write_reg(struct v4l2_subdev *sd,
  342. const struct i2c_regval *msg)
  343. {
  344. while (msg->addr != REG_TERM) {
  345. int ret = cam_i2c_write(sd, msg->addr, msg->val);
  346. if (ret)
  347. return ret;
  348. msg++;
  349. }
  350. return 0;
  351. }
  352. /* Device reset and sleep mode control */
  353. static int sr030pc30_pwr_ctrl(struct v4l2_subdev *sd,
  354. bool reset, bool sleep)
  355. {
  356. struct sr030pc30_info *info = to_sr030pc30(sd);
  357. u8 reg = sleep ? 0xF1 : 0xF0;
  358. int ret = 0;
  359. if (reset)
  360. ret = cam_i2c_write(sd, POWER_CTRL_REG, reg | 0x02);
  361. if (!ret) {
  362. ret = cam_i2c_write(sd, POWER_CTRL_REG, reg);
  363. if (!ret) {
  364. info->sleep = sleep;
  365. if (reset)
  366. info->i2c_reg_page = -1;
  367. }
  368. }
  369. return ret;
  370. }
  371. static inline int sr030pc30_enable_autoexposure(struct v4l2_subdev *sd, int on)
  372. {
  373. struct sr030pc30_info *info = to_sr030pc30(sd);
  374. /* auto anti-flicker is also enabled here */
  375. int ret = cam_i2c_write(sd, AE_CTL1_REG, on ? 0xDC : 0x0C);
  376. if (!ret)
  377. info->auto_exp = on;
  378. return ret;
  379. }
  380. static int sr030pc30_set_exposure(struct v4l2_subdev *sd, int value)
  381. {
  382. struct sr030pc30_info *info = to_sr030pc30(sd);
  383. unsigned long expos = value * info->pdata->clk_rate / (8 * 1000);
  384. int ret = cam_i2c_write(sd, EXP_TIMEH_REG, expos >> 16 & 0xFF);
  385. if (!ret)
  386. ret = cam_i2c_write(sd, EXP_TIMEM_REG, expos >> 8 & 0xFF);
  387. if (!ret)
  388. ret = cam_i2c_write(sd, EXP_TIMEL_REG, expos & 0xFF);
  389. if (!ret) { /* Turn off AE */
  390. info->exposure = value;
  391. ret = sr030pc30_enable_autoexposure(sd, 0);
  392. }
  393. return ret;
  394. }
  395. /* Automatic white balance control */
  396. static int sr030pc30_enable_autowhitebalance(struct v4l2_subdev *sd, int on)
  397. {
  398. struct sr030pc30_info *info = to_sr030pc30(sd);
  399. int ret = cam_i2c_write(sd, AWB_CTL2_REG, on ? 0x2E : 0x2F);
  400. if (!ret)
  401. ret = cam_i2c_write(sd, AWB_CTL1_REG, on ? 0xFB : 0x7B);
  402. if (!ret)
  403. info->auto_wb = on;
  404. return ret;
  405. }
  406. static int sr030pc30_set_flip(struct v4l2_subdev *sd)
  407. {
  408. struct sr030pc30_info *info = to_sr030pc30(sd);
  409. s32 reg = cam_i2c_read(sd, VDO_CTL2_REG);
  410. if (reg < 0)
  411. return reg;
  412. reg &= 0x7C;
  413. if (info->hflip)
  414. reg |= 0x01;
  415. if (info->vflip)
  416. reg |= 0x02;
  417. return cam_i2c_write(sd, VDO_CTL2_REG, reg | 0x80);
  418. }
  419. /* Configure resolution, color format and image flip */
  420. static int sr030pc30_set_params(struct v4l2_subdev *sd)
  421. {
  422. struct sr030pc30_info *info = to_sr030pc30(sd);
  423. int ret;
  424. if (!info->curr_win)
  425. return -EINVAL;
  426. /* Configure the resolution through subsampling */
  427. ret = cam_i2c_write(sd, VDO_CTL1_REG,
  428. info->curr_win->vid_ctl1);
  429. if (!ret && info->curr_fmt)
  430. ret = cam_i2c_write(sd, ISP_CTL_REG(0),
  431. info->curr_fmt->ispctl1_reg);
  432. if (!ret)
  433. ret = sr030pc30_set_flip(sd);
  434. return ret;
  435. }
  436. /* Find nearest matching image pixel size. */
  437. static int sr030pc30_try_frame_size(struct v4l2_mbus_framefmt *mf)
  438. {
  439. unsigned int min_err = ~0;
  440. int i = ARRAY_SIZE(sr030pc30_sizes);
  441. const struct sr030pc30_frmsize *fsize = &sr030pc30_sizes[0],
  442. *match = NULL;
  443. while (i--) {
  444. int err = abs(fsize->width - mf->width)
  445. + abs(fsize->height - mf->height);
  446. if (err < min_err) {
  447. min_err = err;
  448. match = fsize;
  449. }
  450. fsize++;
  451. }
  452. if (match) {
  453. mf->width = match->width;
  454. mf->height = match->height;
  455. return 0;
  456. }
  457. return -EINVAL;
  458. }
  459. static int sr030pc30_queryctrl(struct v4l2_subdev *sd,
  460. struct v4l2_queryctrl *qc)
  461. {
  462. int i;
  463. for (i = 0; i < ARRAY_SIZE(sr030pc30_ctrl); i++)
  464. if (qc->id == sr030pc30_ctrl[i].id) {
  465. *qc = sr030pc30_ctrl[i];
  466. v4l2_dbg(1, debug, sd, "%s id: %d\n",
  467. __func__, qc->id);
  468. return 0;
  469. }
  470. return -EINVAL;
  471. }
  472. static inline int sr030pc30_set_bluebalance(struct v4l2_subdev *sd, int value)
  473. {
  474. int ret = cam_i2c_write(sd, MWB_BGAIN_REG, value);
  475. if (!ret)
  476. to_sr030pc30(sd)->blue_balance = value;
  477. return ret;
  478. }
  479. static inline int sr030pc30_set_redbalance(struct v4l2_subdev *sd, int value)
  480. {
  481. int ret = cam_i2c_write(sd, MWB_RGAIN_REG, value);
  482. if (!ret)
  483. to_sr030pc30(sd)->red_balance = value;
  484. return ret;
  485. }
  486. static int sr030pc30_s_ctrl(struct v4l2_subdev *sd,
  487. struct v4l2_control *ctrl)
  488. {
  489. int i, ret = 0;
  490. for (i = 0; i < ARRAY_SIZE(sr030pc30_ctrl); i++)
  491. if (ctrl->id == sr030pc30_ctrl[i].id)
  492. break;
  493. if (i == ARRAY_SIZE(sr030pc30_ctrl))
  494. return -EINVAL;
  495. if (ctrl->value < sr030pc30_ctrl[i].minimum ||
  496. ctrl->value > sr030pc30_ctrl[i].maximum)
  497. return -ERANGE;
  498. v4l2_dbg(1, debug, sd, "%s: ctrl_id: %d, value: %d\n",
  499. __func__, ctrl->id, ctrl->value);
  500. switch (ctrl->id) {
  501. case V4L2_CID_AUTO_WHITE_BALANCE:
  502. sr030pc30_enable_autowhitebalance(sd, ctrl->value);
  503. break;
  504. case V4L2_CID_BLUE_BALANCE:
  505. ret = sr030pc30_set_bluebalance(sd, ctrl->value);
  506. break;
  507. case V4L2_CID_RED_BALANCE:
  508. ret = sr030pc30_set_redbalance(sd, ctrl->value);
  509. break;
  510. case V4L2_CID_EXPOSURE_AUTO:
  511. sr030pc30_enable_autoexposure(sd,
  512. ctrl->value == V4L2_EXPOSURE_AUTO);
  513. break;
  514. case V4L2_CID_EXPOSURE:
  515. ret = sr030pc30_set_exposure(sd, ctrl->value);
  516. break;
  517. default:
  518. return -EINVAL;
  519. }
  520. return ret;
  521. }
  522. static int sr030pc30_g_ctrl(struct v4l2_subdev *sd,
  523. struct v4l2_control *ctrl)
  524. {
  525. struct sr030pc30_info *info = to_sr030pc30(sd);
  526. v4l2_dbg(1, debug, sd, "%s: id: %d\n", __func__, ctrl->id);
  527. switch (ctrl->id) {
  528. case V4L2_CID_AUTO_WHITE_BALANCE:
  529. ctrl->value = info->auto_wb;
  530. break;
  531. case V4L2_CID_BLUE_BALANCE:
  532. ctrl->value = info->blue_balance;
  533. break;
  534. case V4L2_CID_RED_BALANCE:
  535. ctrl->value = info->red_balance;
  536. break;
  537. case V4L2_CID_EXPOSURE_AUTO:
  538. ctrl->value = info->auto_exp;
  539. break;
  540. case V4L2_CID_EXPOSURE:
  541. ctrl->value = info->exposure;
  542. break;
  543. default:
  544. return -EINVAL;
  545. }
  546. return 0;
  547. }
  548. static int sr030pc30_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  549. enum v4l2_mbus_pixelcode *code)
  550. {
  551. if (!code || index >= ARRAY_SIZE(sr030pc30_formats))
  552. return -EINVAL;
  553. *code = sr030pc30_formats[index].code;
  554. return 0;
  555. }
  556. static int sr030pc30_g_fmt(struct v4l2_subdev *sd,
  557. struct v4l2_mbus_framefmt *mf)
  558. {
  559. struct sr030pc30_info *info = to_sr030pc30(sd);
  560. int ret;
  561. if (!mf)
  562. return -EINVAL;
  563. if (!info->curr_win || !info->curr_fmt) {
  564. ret = sr030pc30_set_params(sd);
  565. if (ret)
  566. return ret;
  567. }
  568. mf->width = info->curr_win->width;
  569. mf->height = info->curr_win->height;
  570. mf->code = info->curr_fmt->code;
  571. mf->colorspace = info->curr_fmt->colorspace;
  572. mf->field = V4L2_FIELD_NONE;
  573. return 0;
  574. }
  575. /* Return nearest media bus frame format. */
  576. static const struct sr030pc30_format *try_fmt(struct v4l2_subdev *sd,
  577. struct v4l2_mbus_framefmt *mf)
  578. {
  579. int i = ARRAY_SIZE(sr030pc30_formats);
  580. sr030pc30_try_frame_size(mf);
  581. while (i--)
  582. if (mf->code == sr030pc30_formats[i].code)
  583. break;
  584. mf->code = sr030pc30_formats[i].code;
  585. return &sr030pc30_formats[i];
  586. }
  587. /* Return nearest media bus frame format. */
  588. static int sr030pc30_try_fmt(struct v4l2_subdev *sd,
  589. struct v4l2_mbus_framefmt *mf)
  590. {
  591. if (!sd || !mf)
  592. return -EINVAL;
  593. try_fmt(sd, mf);
  594. return 0;
  595. }
  596. static int sr030pc30_s_fmt(struct v4l2_subdev *sd,
  597. struct v4l2_mbus_framefmt *mf)
  598. {
  599. struct sr030pc30_info *info = to_sr030pc30(sd);
  600. if (!sd || !mf)
  601. return -EINVAL;
  602. info->curr_fmt = try_fmt(sd, mf);
  603. return sr030pc30_set_params(sd);
  604. }
  605. static int sr030pc30_base_config(struct v4l2_subdev *sd)
  606. {
  607. struct sr030pc30_info *info = to_sr030pc30(sd);
  608. int ret;
  609. unsigned long expmin, expmax;
  610. ret = sr030pc30_bulk_write_reg(sd, sr030pc30_base_regs);
  611. if (!ret) {
  612. info->curr_fmt = &sr030pc30_formats[0];
  613. info->curr_win = &sr030pc30_sizes[0];
  614. ret = sr030pc30_set_params(sd);
  615. }
  616. if (!ret)
  617. ret = sr030pc30_pwr_ctrl(sd, false, false);
  618. if (!ret && !info->pdata)
  619. return ret;
  620. expmin = EXPOS_MIN_MS * info->pdata->clk_rate / (8 * 1000);
  621. expmax = EXPOS_MAX_MS * info->pdata->clk_rate / (8 * 1000);
  622. v4l2_dbg(1, debug, sd, "%s: expmin= %lx, expmax= %lx", __func__,
  623. expmin, expmax);
  624. /* Setting up manual exposure time range */
  625. ret = cam_i2c_write(sd, EXP_MMINH_REG, expmin >> 8 & 0xFF);
  626. if (!ret)
  627. ret = cam_i2c_write(sd, EXP_MMINL_REG, expmin & 0xFF);
  628. if (!ret)
  629. ret = cam_i2c_write(sd, EXP_MMAXH_REG, expmax >> 16 & 0xFF);
  630. if (!ret)
  631. ret = cam_i2c_write(sd, EXP_MMAXM_REG, expmax >> 8 & 0xFF);
  632. if (!ret)
  633. ret = cam_i2c_write(sd, EXP_MMAXL_REG, expmax & 0xFF);
  634. return ret;
  635. }
  636. static int sr030pc30_s_config(struct v4l2_subdev *sd,
  637. int irq, void *platform_data)
  638. {
  639. struct sr030pc30_info *info = to_sr030pc30(sd);
  640. info->pdata = platform_data;
  641. return 0;
  642. }
  643. static int sr030pc30_s_stream(struct v4l2_subdev *sd, int enable)
  644. {
  645. return 0;
  646. }
  647. static int sr030pc30_s_power(struct v4l2_subdev *sd, int on)
  648. {
  649. struct i2c_client *client = v4l2_get_subdevdata(sd);
  650. struct sr030pc30_info *info = to_sr030pc30(sd);
  651. const struct sr030pc30_platform_data *pdata = info->pdata;
  652. int ret;
  653. if (WARN(pdata == NULL, "No platform data!"))
  654. return -ENOMEM;
  655. /*
  656. * Put sensor into power sleep mode before switching off
  657. * power and disabling MCLK.
  658. */
  659. if (!on)
  660. sr030pc30_pwr_ctrl(sd, false, true);
  661. /* set_power controls sensor's power and clock */
  662. if (pdata->set_power) {
  663. ret = pdata->set_power(&client->dev, on);
  664. if (ret)
  665. return ret;
  666. }
  667. if (on) {
  668. ret = sr030pc30_base_config(sd);
  669. } else {
  670. info->curr_win = NULL;
  671. info->curr_fmt = NULL;
  672. }
  673. return ret;
  674. }
  675. static const struct v4l2_subdev_core_ops sr030pc30_core_ops = {
  676. .s_config = sr030pc30_s_config,
  677. .s_power = sr030pc30_s_power,
  678. .queryctrl = sr030pc30_queryctrl,
  679. .s_ctrl = sr030pc30_s_ctrl,
  680. .g_ctrl = sr030pc30_g_ctrl,
  681. };
  682. static const struct v4l2_subdev_video_ops sr030pc30_video_ops = {
  683. .s_stream = sr030pc30_s_stream,
  684. .g_mbus_fmt = sr030pc30_g_fmt,
  685. .s_mbus_fmt = sr030pc30_s_fmt,
  686. .try_mbus_fmt = sr030pc30_try_fmt,
  687. .enum_mbus_fmt = sr030pc30_enum_fmt,
  688. };
  689. static const struct v4l2_subdev_ops sr030pc30_ops = {
  690. .core = &sr030pc30_core_ops,
  691. .video = &sr030pc30_video_ops,
  692. };
  693. /*
  694. * Detect sensor type. Return 0 if SR030PC30 was detected
  695. * or -ENODEV otherwise.
  696. */
  697. static int sr030pc30_detect(struct i2c_client *client)
  698. {
  699. const struct sr030pc30_platform_data *pdata
  700. = client->dev.platform_data;
  701. int ret;
  702. /* Enable sensor's power and clock */
  703. if (pdata->set_power) {
  704. ret = pdata->set_power(&client->dev, 1);
  705. if (ret)
  706. return ret;
  707. }
  708. ret = i2c_smbus_read_byte_data(client, DEVICE_ID_REG);
  709. if (pdata->set_power)
  710. pdata->set_power(&client->dev, 0);
  711. if (ret < 0) {
  712. dev_err(&client->dev, "%s: I2C read failed\n", __func__);
  713. return ret;
  714. }
  715. return ret == SR030PC30_ID ? 0 : -ENODEV;
  716. }
  717. static int sr030pc30_probe(struct i2c_client *client,
  718. const struct i2c_device_id *id)
  719. {
  720. struct sr030pc30_info *info;
  721. struct v4l2_subdev *sd;
  722. const struct sr030pc30_platform_data *pdata
  723. = client->dev.platform_data;
  724. int ret;
  725. if (!pdata) {
  726. dev_err(&client->dev, "No platform data!");
  727. return -EIO;
  728. }
  729. ret = sr030pc30_detect(client);
  730. if (ret)
  731. return ret;
  732. info = kzalloc(sizeof(*info), GFP_KERNEL);
  733. if (!info)
  734. return -ENOMEM;
  735. sd = &info->sd;
  736. strcpy(sd->name, MODULE_NAME);
  737. info->pdata = client->dev.platform_data;
  738. v4l2_i2c_subdev_init(sd, client, &sr030pc30_ops);
  739. info->i2c_reg_page = -1;
  740. info->hflip = 1;
  741. info->auto_exp = 1;
  742. info->exposure = 30;
  743. return 0;
  744. }
  745. static int sr030pc30_remove(struct i2c_client *client)
  746. {
  747. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  748. struct sr030pc30_info *info = to_sr030pc30(sd);
  749. v4l2_device_unregister_subdev(sd);
  750. kfree(info);
  751. return 0;
  752. }
  753. static const struct i2c_device_id sr030pc30_id[] = {
  754. { MODULE_NAME, 0 },
  755. { },
  756. };
  757. MODULE_DEVICE_TABLE(i2c, sr030pc30_id);
  758. static struct i2c_driver sr030pc30_i2c_driver = {
  759. .driver = {
  760. .name = MODULE_NAME
  761. },
  762. .probe = sr030pc30_probe,
  763. .remove = sr030pc30_remove,
  764. .id_table = sr030pc30_id,
  765. };
  766. static int __init sr030pc30_init(void)
  767. {
  768. return i2c_add_driver(&sr030pc30_i2c_driver);
  769. }
  770. static void __exit sr030pc30_exit(void)
  771. {
  772. i2c_del_driver(&sr030pc30_i2c_driver);
  773. }
  774. module_init(sr030pc30_init);
  775. module_exit(sr030pc30_exit);
  776. MODULE_DESCRIPTION("Siliconfile SR030PC30 camera driver");
  777. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  778. MODULE_LICENSE("GPL");