noon010pc30.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /*
  2. * Driver for SiliconFile NOON010PC30 CIF (1/11") Image Sensor with ISP
  3. *
  4. * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd.
  5. * Contact: Sylwester Nawrocki, <s.nawrocki@samsung.com>
  6. *
  7. * Initial register configuration based on a driver authored by
  8. * HeungJun Kim <riverful.kim@samsung.com>.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/gpio.h>
  17. #include <linux/i2c.h>
  18. #include <linux/slab.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <media/noon010pc30.h>
  21. #include <linux/videodev2.h>
  22. #include <linux/module.h>
  23. #include <media/v4l2-ctrls.h>
  24. #include <media/v4l2-device.h>
  25. #include <media/v4l2-mediabus.h>
  26. #include <media/v4l2-subdev.h>
  27. static int debug;
  28. module_param(debug, int, 0644);
  29. MODULE_PARM_DESC(debug, "Enable module debug trace. Set to 1 to enable.");
  30. #define MODULE_NAME "NOON010PC30"
  31. /*
  32. * Register offsets within a page
  33. * b15..b8 - page id, b7..b0 - register address
  34. */
  35. #define POWER_CTRL_REG 0x0001
  36. #define PAGEMODE_REG 0x03
  37. #define DEVICE_ID_REG 0x0004
  38. #define NOON010PC30_ID 0x86
  39. #define VDO_CTL_REG(n) (0x0010 + (n))
  40. #define SYNC_CTL_REG 0x0012
  41. /* Window size and position */
  42. #define WIN_ROWH_REG 0x0013
  43. #define WIN_ROWL_REG 0x0014
  44. #define WIN_COLH_REG 0x0015
  45. #define WIN_COLL_REG 0x0016
  46. #define WIN_HEIGHTH_REG 0x0017
  47. #define WIN_HEIGHTL_REG 0x0018
  48. #define WIN_WIDTHH_REG 0x0019
  49. #define WIN_WIDTHL_REG 0x001A
  50. #define HBLANKH_REG 0x001B
  51. #define HBLANKL_REG 0x001C
  52. #define VSYNCH_REG 0x001D
  53. #define VSYNCL_REG 0x001E
  54. /* VSYNC control */
  55. #define VS_CTL_REG(n) (0x00A1 + (n))
  56. /* page 1 */
  57. #define ISP_CTL_REG(n) (0x0110 + (n))
  58. #define YOFS_REG 0x0119
  59. #define DARK_YOFS_REG 0x011A
  60. #define SAT_CTL_REG 0x0120
  61. #define BSAT_REG 0x0121
  62. #define RSAT_REG 0x0122
  63. /* Color correction */
  64. #define CMC_CTL_REG 0x0130
  65. #define CMC_OFSGH_REG 0x0133
  66. #define CMC_OFSGL_REG 0x0135
  67. #define CMC_SIGN_REG 0x0136
  68. #define CMC_GOFS_REG 0x0137
  69. #define CMC_COEF_REG(n) (0x0138 + (n))
  70. #define CMC_OFS_REG(n) (0x0141 + (n))
  71. /* Gamma correction */
  72. #define GMA_CTL_REG 0x0160
  73. #define GMA_COEF_REG(n) (0x0161 + (n))
  74. /* Lens Shading */
  75. #define LENS_CTRL_REG 0x01D0
  76. #define LENS_XCEN_REG 0x01D1
  77. #define LENS_YCEN_REG 0x01D2
  78. #define LENS_RC_REG 0x01D3
  79. #define LENS_GC_REG 0x01D4
  80. #define LENS_BC_REG 0x01D5
  81. #define L_AGON_REG 0x01D6
  82. #define L_AGOFF_REG 0x01D7
  83. /* Page 3 - Auto Exposure */
  84. #define AE_CTL_REG(n) (0x0310 + (n))
  85. #define AE_CTL9_REG 0x032C
  86. #define AE_CTL10_REG 0x032D
  87. #define AE_YLVL_REG 0x031C
  88. #define AE_YTH_REG(n) (0x031D + (n))
  89. #define AE_WGT_REG 0x0326
  90. #define EXP_TIMEH_REG 0x0333
  91. #define EXP_TIMEM_REG 0x0334
  92. #define EXP_TIMEL_REG 0x0335
  93. #define EXP_MMINH_REG 0x0336
  94. #define EXP_MMINL_REG 0x0337
  95. #define EXP_MMAXH_REG 0x0338
  96. #define EXP_MMAXM_REG 0x0339
  97. #define EXP_MMAXL_REG 0x033A
  98. /* Page 4 - Auto White Balance */
  99. #define AWB_CTL_REG(n) (0x0410 + (n))
  100. #define AWB_ENABE 0x80
  101. #define AWB_WGHT_REG 0x0419
  102. #define BGAIN_PAR_REG(n) (0x044F + (n))
  103. /* Manual white balance, when AWB_CTL2[0]=1 */
  104. #define MWB_RGAIN_REG 0x0466
  105. #define MWB_BGAIN_REG 0x0467
  106. /* The token to mark an array end */
  107. #define REG_TERM 0xFFFF
  108. struct noon010_format {
  109. u32 code;
  110. enum v4l2_colorspace colorspace;
  111. u16 ispctl1_reg;
  112. };
  113. struct noon010_frmsize {
  114. u16 width;
  115. u16 height;
  116. int vid_ctl1;
  117. };
  118. static const char * const noon010_supply_name[] = {
  119. "vdd_core", "vddio", "vdda"
  120. };
  121. #define NOON010_NUM_SUPPLIES ARRAY_SIZE(noon010_supply_name)
  122. struct noon010_info {
  123. struct v4l2_subdev sd;
  124. struct media_pad pad;
  125. struct v4l2_ctrl_handler hdl;
  126. struct regulator_bulk_data supply[NOON010_NUM_SUPPLIES];
  127. u32 gpio_nreset;
  128. u32 gpio_nstby;
  129. /* Protects the struct members below */
  130. struct mutex lock;
  131. const struct noon010_format *curr_fmt;
  132. const struct noon010_frmsize *curr_win;
  133. unsigned int apply_new_cfg:1;
  134. unsigned int streaming:1;
  135. unsigned int hflip:1;
  136. unsigned int vflip:1;
  137. unsigned int power:1;
  138. u8 i2c_reg_page;
  139. };
  140. struct i2c_regval {
  141. u16 addr;
  142. u16 val;
  143. };
  144. /* Supported resolutions. */
  145. static const struct noon010_frmsize noon010_sizes[] = {
  146. {
  147. .width = 352,
  148. .height = 288,
  149. .vid_ctl1 = 0,
  150. }, {
  151. .width = 176,
  152. .height = 144,
  153. .vid_ctl1 = 0x10,
  154. }, {
  155. .width = 88,
  156. .height = 72,
  157. .vid_ctl1 = 0x20,
  158. },
  159. };
  160. /* Supported pixel formats. */
  161. static const struct noon010_format noon010_formats[] = {
  162. {
  163. .code = MEDIA_BUS_FMT_YUYV8_2X8,
  164. .colorspace = V4L2_COLORSPACE_JPEG,
  165. .ispctl1_reg = 0x03,
  166. }, {
  167. .code = MEDIA_BUS_FMT_YVYU8_2X8,
  168. .colorspace = V4L2_COLORSPACE_JPEG,
  169. .ispctl1_reg = 0x02,
  170. }, {
  171. .code = MEDIA_BUS_FMT_VYUY8_2X8,
  172. .colorspace = V4L2_COLORSPACE_JPEG,
  173. .ispctl1_reg = 0,
  174. }, {
  175. .code = MEDIA_BUS_FMT_UYVY8_2X8,
  176. .colorspace = V4L2_COLORSPACE_JPEG,
  177. .ispctl1_reg = 0x01,
  178. }, {
  179. .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
  180. .colorspace = V4L2_COLORSPACE_JPEG,
  181. .ispctl1_reg = 0x40,
  182. },
  183. };
  184. static const struct i2c_regval noon010_base_regs[] = {
  185. { WIN_COLL_REG, 0x06 }, { HBLANKL_REG, 0x7C },
  186. /* Color corection and saturation */
  187. { ISP_CTL_REG(0), 0x30 }, { ISP_CTL_REG(2), 0x30 },
  188. { YOFS_REG, 0x80 }, { DARK_YOFS_REG, 0x04 },
  189. { SAT_CTL_REG, 0x1F }, { BSAT_REG, 0x90 },
  190. { CMC_CTL_REG, 0x0F }, { CMC_OFSGH_REG, 0x3C },
  191. { CMC_OFSGL_REG, 0x2C }, { CMC_SIGN_REG, 0x3F },
  192. { CMC_COEF_REG(0), 0x79 }, { CMC_OFS_REG(0), 0x00 },
  193. { CMC_COEF_REG(1), 0x39 }, { CMC_OFS_REG(1), 0x00 },
  194. { CMC_COEF_REG(2), 0x00 }, { CMC_OFS_REG(2), 0x00 },
  195. { CMC_COEF_REG(3), 0x11 }, { CMC_OFS_REG(3), 0x8B },
  196. { CMC_COEF_REG(4), 0x65 }, { CMC_OFS_REG(4), 0x07 },
  197. { CMC_COEF_REG(5), 0x14 }, { CMC_OFS_REG(5), 0x04 },
  198. { CMC_COEF_REG(6), 0x01 }, { CMC_OFS_REG(6), 0x9C },
  199. { CMC_COEF_REG(7), 0x33 }, { CMC_OFS_REG(7), 0x89 },
  200. { CMC_COEF_REG(8), 0x74 }, { CMC_OFS_REG(8), 0x25 },
  201. /* Automatic white balance */
  202. { AWB_CTL_REG(0), 0x78 }, { AWB_CTL_REG(1), 0x2E },
  203. { AWB_CTL_REG(2), 0x20 }, { AWB_CTL_REG(3), 0x85 },
  204. /* Auto exposure */
  205. { AE_CTL_REG(0), 0xDC }, { AE_CTL_REG(1), 0x81 },
  206. { AE_CTL_REG(2), 0x30 }, { AE_CTL_REG(3), 0xA5 },
  207. { AE_CTL_REG(4), 0x40 }, { AE_CTL_REG(5), 0x51 },
  208. { AE_CTL_REG(6), 0x33 }, { AE_CTL_REG(7), 0x7E },
  209. { AE_CTL9_REG, 0x00 }, { AE_CTL10_REG, 0x02 },
  210. { AE_YLVL_REG, 0x44 }, { AE_YTH_REG(0), 0x34 },
  211. { AE_YTH_REG(1), 0x30 }, { AE_WGT_REG, 0xD5 },
  212. /* Lens shading compensation */
  213. { LENS_CTRL_REG, 0x01 }, { LENS_XCEN_REG, 0x80 },
  214. { LENS_YCEN_REG, 0x70 }, { LENS_RC_REG, 0x53 },
  215. { LENS_GC_REG, 0x40 }, { LENS_BC_REG, 0x3E },
  216. { REG_TERM, 0 },
  217. };
  218. static inline struct noon010_info *to_noon010(struct v4l2_subdev *sd)
  219. {
  220. return container_of(sd, struct noon010_info, sd);
  221. }
  222. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  223. {
  224. return &container_of(ctrl->handler, struct noon010_info, hdl)->sd;
  225. }
  226. static inline int set_i2c_page(struct noon010_info *info,
  227. struct i2c_client *client, unsigned int reg)
  228. {
  229. u32 page = reg >> 8 & 0xFF;
  230. int ret = 0;
  231. if (info->i2c_reg_page != page && (reg & 0xFF) != 0x03) {
  232. ret = i2c_smbus_write_byte_data(client, PAGEMODE_REG, page);
  233. if (!ret)
  234. info->i2c_reg_page = page;
  235. }
  236. return ret;
  237. }
  238. static int cam_i2c_read(struct v4l2_subdev *sd, u32 reg_addr)
  239. {
  240. struct i2c_client *client = v4l2_get_subdevdata(sd);
  241. struct noon010_info *info = to_noon010(sd);
  242. int ret = set_i2c_page(info, client, reg_addr);
  243. if (ret)
  244. return ret;
  245. return i2c_smbus_read_byte_data(client, reg_addr & 0xFF);
  246. }
  247. static int cam_i2c_write(struct v4l2_subdev *sd, u32 reg_addr, u32 val)
  248. {
  249. struct i2c_client *client = v4l2_get_subdevdata(sd);
  250. struct noon010_info *info = to_noon010(sd);
  251. int ret = set_i2c_page(info, client, reg_addr);
  252. if (ret)
  253. return ret;
  254. return i2c_smbus_write_byte_data(client, reg_addr & 0xFF, val);
  255. }
  256. static inline int noon010_bulk_write_reg(struct v4l2_subdev *sd,
  257. const struct i2c_regval *msg)
  258. {
  259. while (msg->addr != REG_TERM) {
  260. int ret = cam_i2c_write(sd, msg->addr, msg->val);
  261. if (ret)
  262. return ret;
  263. msg++;
  264. }
  265. return 0;
  266. }
  267. /* Device reset and sleep mode control */
  268. static int noon010_power_ctrl(struct v4l2_subdev *sd, bool reset, bool sleep)
  269. {
  270. struct noon010_info *info = to_noon010(sd);
  271. u8 reg = sleep ? 0xF1 : 0xF0;
  272. int ret = 0;
  273. if (reset) {
  274. ret = cam_i2c_write(sd, POWER_CTRL_REG, reg | 0x02);
  275. udelay(20);
  276. }
  277. if (!ret) {
  278. ret = cam_i2c_write(sd, POWER_CTRL_REG, reg);
  279. if (reset && !ret)
  280. info->i2c_reg_page = -1;
  281. }
  282. return ret;
  283. }
  284. /* Automatic white balance control */
  285. static int noon010_enable_autowhitebalance(struct v4l2_subdev *sd, int on)
  286. {
  287. int ret;
  288. ret = cam_i2c_write(sd, AWB_CTL_REG(1), on ? 0x2E : 0x2F);
  289. if (!ret)
  290. ret = cam_i2c_write(sd, AWB_CTL_REG(0), on ? 0xFB : 0x7B);
  291. return ret;
  292. }
  293. /* Called with struct noon010_info.lock mutex held */
  294. static int noon010_set_flip(struct v4l2_subdev *sd, int hflip, int vflip)
  295. {
  296. struct noon010_info *info = to_noon010(sd);
  297. int reg, ret;
  298. reg = cam_i2c_read(sd, VDO_CTL_REG(1));
  299. if (reg < 0)
  300. return reg;
  301. reg &= 0x7C;
  302. if (hflip)
  303. reg |= 0x01;
  304. if (vflip)
  305. reg |= 0x02;
  306. ret = cam_i2c_write(sd, VDO_CTL_REG(1), reg | 0x80);
  307. if (!ret) {
  308. info->hflip = hflip;
  309. info->vflip = vflip;
  310. }
  311. return ret;
  312. }
  313. /* Configure resolution and color format */
  314. static int noon010_set_params(struct v4l2_subdev *sd)
  315. {
  316. struct noon010_info *info = to_noon010(sd);
  317. int ret = cam_i2c_write(sd, VDO_CTL_REG(0),
  318. info->curr_win->vid_ctl1);
  319. if (ret)
  320. return ret;
  321. return cam_i2c_write(sd, ISP_CTL_REG(0),
  322. info->curr_fmt->ispctl1_reg);
  323. }
  324. /* Find nearest matching image pixel size. */
  325. static int noon010_try_frame_size(struct v4l2_mbus_framefmt *mf,
  326. const struct noon010_frmsize **size)
  327. {
  328. unsigned int min_err = ~0;
  329. int i = ARRAY_SIZE(noon010_sizes);
  330. const struct noon010_frmsize *fsize = &noon010_sizes[0],
  331. *match = NULL;
  332. while (i--) {
  333. int err = abs(fsize->width - mf->width)
  334. + abs(fsize->height - mf->height);
  335. if (err < min_err) {
  336. min_err = err;
  337. match = fsize;
  338. }
  339. fsize++;
  340. }
  341. if (match) {
  342. mf->width = match->width;
  343. mf->height = match->height;
  344. if (size)
  345. *size = match;
  346. return 0;
  347. }
  348. return -EINVAL;
  349. }
  350. /* Called with info.lock mutex held */
  351. static int power_enable(struct noon010_info *info)
  352. {
  353. int ret;
  354. if (info->power) {
  355. v4l2_info(&info->sd, "%s: sensor is already on\n", __func__);
  356. return 0;
  357. }
  358. if (gpio_is_valid(info->gpio_nstby))
  359. gpio_set_value(info->gpio_nstby, 0);
  360. if (gpio_is_valid(info->gpio_nreset))
  361. gpio_set_value(info->gpio_nreset, 0);
  362. ret = regulator_bulk_enable(NOON010_NUM_SUPPLIES, info->supply);
  363. if (ret)
  364. return ret;
  365. if (gpio_is_valid(info->gpio_nreset)) {
  366. msleep(50);
  367. gpio_set_value(info->gpio_nreset, 1);
  368. }
  369. if (gpio_is_valid(info->gpio_nstby)) {
  370. udelay(1000);
  371. gpio_set_value(info->gpio_nstby, 1);
  372. }
  373. if (gpio_is_valid(info->gpio_nreset)) {
  374. udelay(1000);
  375. gpio_set_value(info->gpio_nreset, 0);
  376. msleep(100);
  377. gpio_set_value(info->gpio_nreset, 1);
  378. msleep(20);
  379. }
  380. info->power = 1;
  381. v4l2_dbg(1, debug, &info->sd, "%s: sensor is on\n", __func__);
  382. return 0;
  383. }
  384. /* Called with info.lock mutex held */
  385. static int power_disable(struct noon010_info *info)
  386. {
  387. int ret;
  388. if (!info->power) {
  389. v4l2_info(&info->sd, "%s: sensor is already off\n", __func__);
  390. return 0;
  391. }
  392. ret = regulator_bulk_disable(NOON010_NUM_SUPPLIES, info->supply);
  393. if (ret)
  394. return ret;
  395. if (gpio_is_valid(info->gpio_nstby))
  396. gpio_set_value(info->gpio_nstby, 0);
  397. if (gpio_is_valid(info->gpio_nreset))
  398. gpio_set_value(info->gpio_nreset, 0);
  399. info->power = 0;
  400. v4l2_dbg(1, debug, &info->sd, "%s: sensor is off\n", __func__);
  401. return 0;
  402. }
  403. static int noon010_s_ctrl(struct v4l2_ctrl *ctrl)
  404. {
  405. struct v4l2_subdev *sd = to_sd(ctrl);
  406. struct noon010_info *info = to_noon010(sd);
  407. int ret = 0;
  408. v4l2_dbg(1, debug, sd, "%s: ctrl_id: %d, value: %d\n",
  409. __func__, ctrl->id, ctrl->val);
  410. mutex_lock(&info->lock);
  411. /*
  412. * If the device is not powered up by the host driver do
  413. * not apply any controls to H/W at this time. Instead
  414. * the controls will be restored right after power-up.
  415. */
  416. if (!info->power)
  417. goto unlock;
  418. switch (ctrl->id) {
  419. case V4L2_CID_AUTO_WHITE_BALANCE:
  420. ret = noon010_enable_autowhitebalance(sd, ctrl->val);
  421. break;
  422. case V4L2_CID_BLUE_BALANCE:
  423. ret = cam_i2c_write(sd, MWB_BGAIN_REG, ctrl->val);
  424. break;
  425. case V4L2_CID_RED_BALANCE:
  426. ret = cam_i2c_write(sd, MWB_RGAIN_REG, ctrl->val);
  427. break;
  428. default:
  429. ret = -EINVAL;
  430. }
  431. unlock:
  432. mutex_unlock(&info->lock);
  433. return ret;
  434. }
  435. static int noon010_enum_mbus_code(struct v4l2_subdev *sd,
  436. struct v4l2_subdev_pad_config *cfg,
  437. struct v4l2_subdev_mbus_code_enum *code)
  438. {
  439. if (code->index >= ARRAY_SIZE(noon010_formats))
  440. return -EINVAL;
  441. code->code = noon010_formats[code->index].code;
  442. return 0;
  443. }
  444. static int noon010_get_fmt(struct v4l2_subdev *sd,
  445. struct v4l2_subdev_pad_config *cfg,
  446. struct v4l2_subdev_format *fmt)
  447. {
  448. struct noon010_info *info = to_noon010(sd);
  449. struct v4l2_mbus_framefmt *mf;
  450. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  451. if (cfg) {
  452. mf = v4l2_subdev_get_try_format(sd, cfg, 0);
  453. fmt->format = *mf;
  454. }
  455. return 0;
  456. }
  457. mf = &fmt->format;
  458. mutex_lock(&info->lock);
  459. mf->width = info->curr_win->width;
  460. mf->height = info->curr_win->height;
  461. mf->code = info->curr_fmt->code;
  462. mf->colorspace = info->curr_fmt->colorspace;
  463. mf->field = V4L2_FIELD_NONE;
  464. mutex_unlock(&info->lock);
  465. return 0;
  466. }
  467. /* Return nearest media bus frame format. */
  468. static const struct noon010_format *noon010_try_fmt(struct v4l2_subdev *sd,
  469. struct v4l2_mbus_framefmt *mf)
  470. {
  471. int i = ARRAY_SIZE(noon010_formats);
  472. while (--i)
  473. if (mf->code == noon010_formats[i].code)
  474. break;
  475. mf->code = noon010_formats[i].code;
  476. return &noon010_formats[i];
  477. }
  478. static int noon010_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
  479. struct v4l2_subdev_format *fmt)
  480. {
  481. struct noon010_info *info = to_noon010(sd);
  482. const struct noon010_frmsize *size = NULL;
  483. const struct noon010_format *nf;
  484. struct v4l2_mbus_framefmt *mf;
  485. int ret = 0;
  486. nf = noon010_try_fmt(sd, &fmt->format);
  487. noon010_try_frame_size(&fmt->format, &size);
  488. fmt->format.colorspace = V4L2_COLORSPACE_JPEG;
  489. fmt->format.field = V4L2_FIELD_NONE;
  490. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  491. if (cfg) {
  492. mf = v4l2_subdev_get_try_format(sd, cfg, 0);
  493. *mf = fmt->format;
  494. }
  495. return 0;
  496. }
  497. mutex_lock(&info->lock);
  498. if (!info->streaming) {
  499. info->apply_new_cfg = 1;
  500. info->curr_fmt = nf;
  501. info->curr_win = size;
  502. } else {
  503. ret = -EBUSY;
  504. }
  505. mutex_unlock(&info->lock);
  506. return ret;
  507. }
  508. /* Called with struct noon010_info.lock mutex held */
  509. static int noon010_base_config(struct v4l2_subdev *sd)
  510. {
  511. int ret = noon010_bulk_write_reg(sd, noon010_base_regs);
  512. if (!ret)
  513. ret = noon010_set_params(sd);
  514. if (!ret)
  515. ret = noon010_set_flip(sd, 1, 0);
  516. return ret;
  517. }
  518. static int noon010_s_power(struct v4l2_subdev *sd, int on)
  519. {
  520. struct noon010_info *info = to_noon010(sd);
  521. int ret;
  522. mutex_lock(&info->lock);
  523. if (on) {
  524. ret = power_enable(info);
  525. if (!ret)
  526. ret = noon010_base_config(sd);
  527. } else {
  528. noon010_power_ctrl(sd, false, true);
  529. ret = power_disable(info);
  530. }
  531. mutex_unlock(&info->lock);
  532. /* Restore the controls state */
  533. if (!ret && on)
  534. ret = v4l2_ctrl_handler_setup(&info->hdl);
  535. return ret;
  536. }
  537. static int noon010_s_stream(struct v4l2_subdev *sd, int on)
  538. {
  539. struct noon010_info *info = to_noon010(sd);
  540. int ret = 0;
  541. mutex_lock(&info->lock);
  542. if (!info->streaming != !on) {
  543. ret = noon010_power_ctrl(sd, false, !on);
  544. if (!ret)
  545. info->streaming = on;
  546. }
  547. if (!ret && on && info->apply_new_cfg) {
  548. ret = noon010_set_params(sd);
  549. if (!ret)
  550. info->apply_new_cfg = 0;
  551. }
  552. mutex_unlock(&info->lock);
  553. return ret;
  554. }
  555. static int noon010_log_status(struct v4l2_subdev *sd)
  556. {
  557. struct noon010_info *info = to_noon010(sd);
  558. v4l2_ctrl_handler_log_status(&info->hdl, sd->name);
  559. return 0;
  560. }
  561. static int noon010_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  562. {
  563. struct v4l2_mbus_framefmt *mf = v4l2_subdev_get_try_format(sd, fh->pad, 0);
  564. mf->width = noon010_sizes[0].width;
  565. mf->height = noon010_sizes[0].height;
  566. mf->code = noon010_formats[0].code;
  567. mf->colorspace = V4L2_COLORSPACE_JPEG;
  568. mf->field = V4L2_FIELD_NONE;
  569. return 0;
  570. }
  571. static const struct v4l2_subdev_internal_ops noon010_subdev_internal_ops = {
  572. .open = noon010_open,
  573. };
  574. static const struct v4l2_ctrl_ops noon010_ctrl_ops = {
  575. .s_ctrl = noon010_s_ctrl,
  576. };
  577. static const struct v4l2_subdev_core_ops noon010_core_ops = {
  578. .s_power = noon010_s_power,
  579. .log_status = noon010_log_status,
  580. };
  581. static struct v4l2_subdev_pad_ops noon010_pad_ops = {
  582. .enum_mbus_code = noon010_enum_mbus_code,
  583. .get_fmt = noon010_get_fmt,
  584. .set_fmt = noon010_set_fmt,
  585. };
  586. static struct v4l2_subdev_video_ops noon010_video_ops = {
  587. .s_stream = noon010_s_stream,
  588. };
  589. static const struct v4l2_subdev_ops noon010_ops = {
  590. .core = &noon010_core_ops,
  591. .pad = &noon010_pad_ops,
  592. .video = &noon010_video_ops,
  593. };
  594. /* Return 0 if NOON010PC30L sensor type was detected or -ENODEV otherwise. */
  595. static int noon010_detect(struct i2c_client *client, struct noon010_info *info)
  596. {
  597. int ret;
  598. ret = power_enable(info);
  599. if (ret)
  600. return ret;
  601. ret = i2c_smbus_read_byte_data(client, DEVICE_ID_REG);
  602. if (ret < 0)
  603. dev_err(&client->dev, "I2C read failed: 0x%X\n", ret);
  604. power_disable(info);
  605. return ret == NOON010PC30_ID ? 0 : -ENODEV;
  606. }
  607. static int noon010_probe(struct i2c_client *client,
  608. const struct i2c_device_id *id)
  609. {
  610. struct noon010_info *info;
  611. struct v4l2_subdev *sd;
  612. const struct noon010pc30_platform_data *pdata
  613. = client->dev.platform_data;
  614. int ret;
  615. int i;
  616. if (!pdata) {
  617. dev_err(&client->dev, "No platform data!\n");
  618. return -EIO;
  619. }
  620. info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
  621. if (!info)
  622. return -ENOMEM;
  623. mutex_init(&info->lock);
  624. sd = &info->sd;
  625. v4l2_i2c_subdev_init(sd, client, &noon010_ops);
  626. strlcpy(sd->name, MODULE_NAME, sizeof(sd->name));
  627. sd->internal_ops = &noon010_subdev_internal_ops;
  628. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  629. v4l2_ctrl_handler_init(&info->hdl, 3);
  630. v4l2_ctrl_new_std(&info->hdl, &noon010_ctrl_ops,
  631. V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
  632. v4l2_ctrl_new_std(&info->hdl, &noon010_ctrl_ops,
  633. V4L2_CID_RED_BALANCE, 0, 127, 1, 64);
  634. v4l2_ctrl_new_std(&info->hdl, &noon010_ctrl_ops,
  635. V4L2_CID_BLUE_BALANCE, 0, 127, 1, 64);
  636. sd->ctrl_handler = &info->hdl;
  637. ret = info->hdl.error;
  638. if (ret)
  639. goto np_err;
  640. info->i2c_reg_page = -1;
  641. info->gpio_nreset = -EINVAL;
  642. info->gpio_nstby = -EINVAL;
  643. info->curr_fmt = &noon010_formats[0];
  644. info->curr_win = &noon010_sizes[0];
  645. if (gpio_is_valid(pdata->gpio_nreset)) {
  646. ret = devm_gpio_request_one(&client->dev, pdata->gpio_nreset,
  647. GPIOF_OUT_INIT_LOW,
  648. "NOON010PC30 NRST");
  649. if (ret) {
  650. dev_err(&client->dev, "GPIO request error: %d\n", ret);
  651. goto np_err;
  652. }
  653. info->gpio_nreset = pdata->gpio_nreset;
  654. gpio_export(info->gpio_nreset, 0);
  655. }
  656. if (gpio_is_valid(pdata->gpio_nstby)) {
  657. ret = devm_gpio_request_one(&client->dev, pdata->gpio_nstby,
  658. GPIOF_OUT_INIT_LOW,
  659. "NOON010PC30 NSTBY");
  660. if (ret) {
  661. dev_err(&client->dev, "GPIO request error: %d\n", ret);
  662. goto np_err;
  663. }
  664. info->gpio_nstby = pdata->gpio_nstby;
  665. gpio_export(info->gpio_nstby, 0);
  666. }
  667. for (i = 0; i < NOON010_NUM_SUPPLIES; i++)
  668. info->supply[i].supply = noon010_supply_name[i];
  669. ret = devm_regulator_bulk_get(&client->dev, NOON010_NUM_SUPPLIES,
  670. info->supply);
  671. if (ret)
  672. goto np_err;
  673. info->pad.flags = MEDIA_PAD_FL_SOURCE;
  674. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
  675. ret = media_entity_init(&sd->entity, 1, &info->pad, 0);
  676. if (ret < 0)
  677. goto np_err;
  678. ret = noon010_detect(client, info);
  679. if (!ret)
  680. return 0;
  681. np_err:
  682. v4l2_ctrl_handler_free(&info->hdl);
  683. v4l2_device_unregister_subdev(sd);
  684. return ret;
  685. }
  686. static int noon010_remove(struct i2c_client *client)
  687. {
  688. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  689. struct noon010_info *info = to_noon010(sd);
  690. v4l2_device_unregister_subdev(sd);
  691. v4l2_ctrl_handler_free(&info->hdl);
  692. media_entity_cleanup(&sd->entity);
  693. return 0;
  694. }
  695. static const struct i2c_device_id noon010_id[] = {
  696. { MODULE_NAME, 0 },
  697. { },
  698. };
  699. MODULE_DEVICE_TABLE(i2c, noon010_id);
  700. static struct i2c_driver noon010_i2c_driver = {
  701. .driver = {
  702. .name = MODULE_NAME
  703. },
  704. .probe = noon010_probe,
  705. .remove = noon010_remove,
  706. .id_table = noon010_id,
  707. };
  708. module_i2c_driver(noon010_i2c_driver);
  709. MODULE_DESCRIPTION("Siliconfile NOON010PC30 camera driver");
  710. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  711. MODULE_LICENSE("GPL");