PageRenderTime 185ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/media/video/noon010pc30.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 792 lines | 627 code | 123 blank | 42 comment | 75 complexity | fc0ff08d76260b04b35e5241d1888e3a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Driver for SiliconFile NOON010PC30 CIF (1/11") Image Sensor with ISP
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  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 vergsion.
  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 <media/v4l2-chip-ident.h>
  22. #include <linux/videodev2.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. enum v4l2_mbus_pixelcode 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 v4l2_ctrl_handler hdl;
  125. const struct noon010pc30_platform_data *pdata;
  126. const struct noon010_format *curr_fmt;
  127. const struct noon010_frmsize *curr_win;
  128. unsigned int hflip:1;
  129. unsigned int vflip:1;
  130. unsigned int power:1;
  131. u8 i2c_reg_page;
  132. struct regulator_bulk_data supply[NOON010_NUM_SUPPLIES];
  133. u32 gpio_nreset;
  134. u32 gpio_nstby;
  135. };
  136. struct i2c_regval {
  137. u16 addr;
  138. u16 val;
  139. };
  140. /* Supported resolutions. */
  141. static const struct noon010_frmsize noon010_sizes[] = {
  142. {
  143. .width = 352,
  144. .height = 288,
  145. .vid_ctl1 = 0,
  146. }, {
  147. .width = 176,
  148. .height = 144,
  149. .vid_ctl1 = 0x10,
  150. }, {
  151. .width = 88,
  152. .height = 72,
  153. .vid_ctl1 = 0x20,
  154. },
  155. };
  156. /* Supported pixel formats. */
  157. static const struct noon010_format noon010_formats[] = {
  158. {
  159. .code = V4L2_MBUS_FMT_YUYV8_2X8,
  160. .colorspace = V4L2_COLORSPACE_JPEG,
  161. .ispctl1_reg = 0x03,
  162. }, {
  163. .code = V4L2_MBUS_FMT_YVYU8_2X8,
  164. .colorspace = V4L2_COLORSPACE_JPEG,
  165. .ispctl1_reg = 0x02,
  166. }, {
  167. .code = V4L2_MBUS_FMT_VYUY8_2X8,
  168. .colorspace = V4L2_COLORSPACE_JPEG,
  169. .ispctl1_reg = 0,
  170. }, {
  171. .code = V4L2_MBUS_FMT_UYVY8_2X8,
  172. .colorspace = V4L2_COLORSPACE_JPEG,
  173. .ispctl1_reg = 0x01,
  174. }, {
  175. .code = V4L2_MBUS_FMT_RGB565_2X8_BE,
  176. .colorspace = V4L2_COLORSPACE_JPEG,
  177. .ispctl1_reg = 0x40,
  178. },
  179. };
  180. static const struct i2c_regval noon010_base_regs[] = {
  181. { WIN_COLL_REG, 0x06 }, { HBLANKL_REG, 0x7C },
  182. /* Color corection and saturation */
  183. { ISP_CTL_REG(0), 0x30 }, { ISP_CTL_REG(2), 0x30 },
  184. { YOFS_REG, 0x80 }, { DARK_YOFS_REG, 0x04 },
  185. { SAT_CTL_REG, 0x1F }, { BSAT_REG, 0x90 },
  186. { CMC_CTL_REG, 0x0F }, { CMC_OFSGH_REG, 0x3C },
  187. { CMC_OFSGL_REG, 0x2C }, { CMC_SIGN_REG, 0x3F },
  188. { CMC_COEF_REG(0), 0x79 }, { CMC_OFS_REG(0), 0x00 },
  189. { CMC_COEF_REG(1), 0x39 }, { CMC_OFS_REG(1), 0x00 },
  190. { CMC_COEF_REG(2), 0x00 }, { CMC_OFS_REG(2), 0x00 },
  191. { CMC_COEF_REG(3), 0x11 }, { CMC_OFS_REG(3), 0x8B },
  192. { CMC_COEF_REG(4), 0x65 }, { CMC_OFS_REG(4), 0x07 },
  193. { CMC_COEF_REG(5), 0x14 }, { CMC_OFS_REG(5), 0x04 },
  194. { CMC_COEF_REG(6), 0x01 }, { CMC_OFS_REG(6), 0x9C },
  195. { CMC_COEF_REG(7), 0x33 }, { CMC_OFS_REG(7), 0x89 },
  196. { CMC_COEF_REG(8), 0x74 }, { CMC_OFS_REG(8), 0x25 },
  197. /* Automatic white balance */
  198. { AWB_CTL_REG(0), 0x78 }, { AWB_CTL_REG(1), 0x2E },
  199. { AWB_CTL_REG(2), 0x20 }, { AWB_CTL_REG(3), 0x85 },
  200. /* Auto exposure */
  201. { AE_CTL_REG(0), 0xDC }, { AE_CTL_REG(1), 0x81 },
  202. { AE_CTL_REG(2), 0x30 }, { AE_CTL_REG(3), 0xA5 },
  203. { AE_CTL_REG(4), 0x40 }, { AE_CTL_REG(5), 0x51 },
  204. { AE_CTL_REG(6), 0x33 }, { AE_CTL_REG(7), 0x7E },
  205. { AE_CTL9_REG, 0x00 }, { AE_CTL10_REG, 0x02 },
  206. { AE_YLVL_REG, 0x44 }, { AE_YTH_REG(0), 0x34 },
  207. { AE_YTH_REG(1), 0x30 }, { AE_WGT_REG, 0xD5 },
  208. /* Lens shading compensation */
  209. { LENS_CTRL_REG, 0x01 }, { LENS_XCEN_REG, 0x80 },
  210. { LENS_YCEN_REG, 0x70 }, { LENS_RC_REG, 0x53 },
  211. { LENS_GC_REG, 0x40 }, { LENS_BC_REG, 0x3E },
  212. { REG_TERM, 0 },
  213. };
  214. static inline struct noon010_info *to_noon010(struct v4l2_subdev *sd)
  215. {
  216. return container_of(sd, struct noon010_info, sd);
  217. }
  218. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  219. {
  220. return &container_of(ctrl->handler, struct noon010_info, hdl)->sd;
  221. }
  222. static inline int set_i2c_page(struct noon010_info *info,
  223. struct i2c_client *client, unsigned int reg)
  224. {
  225. u32 page = reg >> 8 & 0xFF;
  226. int ret = 0;
  227. if (info->i2c_reg_page != page && (reg & 0xFF) != 0x03) {
  228. ret = i2c_smbus_write_byte_data(client, PAGEMODE_REG, page);
  229. if (!ret)
  230. info->i2c_reg_page = page;
  231. }
  232. return ret;
  233. }
  234. static int cam_i2c_read(struct v4l2_subdev *sd, u32 reg_addr)
  235. {
  236. struct i2c_client *client = v4l2_get_subdevdata(sd);
  237. struct noon010_info *info = to_noon010(sd);
  238. int ret = set_i2c_page(info, client, reg_addr);
  239. if (ret)
  240. return ret;
  241. return i2c_smbus_read_byte_data(client, reg_addr & 0xFF);
  242. }
  243. static int cam_i2c_write(struct v4l2_subdev *sd, u32 reg_addr, u32 val)
  244. {
  245. struct i2c_client *client = v4l2_get_subdevdata(sd);
  246. struct noon010_info *info = to_noon010(sd);
  247. int ret = set_i2c_page(info, client, reg_addr);
  248. if (ret)
  249. return ret;
  250. return i2c_smbus_write_byte_data(client, reg_addr & 0xFF, val);
  251. }
  252. static inline int noon010_bulk_write_reg(struct v4l2_subdev *sd,
  253. const struct i2c_regval *msg)
  254. {
  255. while (msg->addr != REG_TERM) {
  256. int ret = cam_i2c_write(sd, msg->addr, msg->val);
  257. if (ret)
  258. return ret;
  259. msg++;
  260. }
  261. return 0;
  262. }
  263. /* Device reset and sleep mode control */
  264. static int noon010_power_ctrl(struct v4l2_subdev *sd, bool reset, bool sleep)
  265. {
  266. struct noon010_info *info = to_noon010(sd);
  267. u8 reg = sleep ? 0xF1 : 0xF0;
  268. int ret = 0;
  269. if (reset)
  270. ret = cam_i2c_write(sd, POWER_CTRL_REG, reg | 0x02);
  271. if (!ret) {
  272. ret = cam_i2c_write(sd, POWER_CTRL_REG, reg);
  273. if (reset && !ret)
  274. info->i2c_reg_page = -1;
  275. }
  276. return ret;
  277. }
  278. /* Automatic white balance control */
  279. static int noon010_enable_autowhitebalance(struct v4l2_subdev *sd, int on)
  280. {
  281. int ret;
  282. ret = cam_i2c_write(sd, AWB_CTL_REG(1), on ? 0x2E : 0x2F);
  283. if (!ret)
  284. ret = cam_i2c_write(sd, AWB_CTL_REG(0), on ? 0xFB : 0x7B);
  285. return ret;
  286. }
  287. static int noon010_set_flip(struct v4l2_subdev *sd, int hflip, int vflip)
  288. {
  289. struct noon010_info *info = to_noon010(sd);
  290. int reg, ret;
  291. reg = cam_i2c_read(sd, VDO_CTL_REG(1));
  292. if (reg < 0)
  293. return reg;
  294. reg &= 0x7C;
  295. if (hflip)
  296. reg |= 0x01;
  297. if (vflip)
  298. reg |= 0x02;
  299. ret = cam_i2c_write(sd, VDO_CTL_REG(1), reg | 0x80);
  300. if (!ret) {
  301. info->hflip = hflip;
  302. info->vflip = vflip;
  303. }
  304. return ret;
  305. }
  306. /* Configure resolution and color format */
  307. static int noon010_set_params(struct v4l2_subdev *sd)
  308. {
  309. struct noon010_info *info = to_noon010(sd);
  310. int ret;
  311. if (!info->curr_win)
  312. return -EINVAL;
  313. ret = cam_i2c_write(sd, VDO_CTL_REG(0), info->curr_win->vid_ctl1);
  314. if (!ret && info->curr_fmt)
  315. ret = cam_i2c_write(sd, ISP_CTL_REG(0),
  316. info->curr_fmt->ispctl1_reg);
  317. return ret;
  318. }
  319. /* Find nearest matching image pixel size. */
  320. static int noon010_try_frame_size(struct v4l2_mbus_framefmt *mf)
  321. {
  322. unsigned int min_err = ~0;
  323. int i = ARRAY_SIZE(noon010_sizes);
  324. const struct noon010_frmsize *fsize = &noon010_sizes[0],
  325. *match = NULL;
  326. while (i--) {
  327. int err = abs(fsize->width - mf->width)
  328. + abs(fsize->height - mf->height);
  329. if (err < min_err) {
  330. min_err = err;
  331. match = fsize;
  332. }
  333. fsize++;
  334. }
  335. if (match) {
  336. mf->width = match->width;
  337. mf->height = match->height;
  338. return 0;
  339. }
  340. return -EINVAL;
  341. }
  342. static int power_enable(struct noon010_info *info)
  343. {
  344. int ret;
  345. if (info->power) {
  346. v4l2_info(&info->sd, "%s: sensor is already on\n", __func__);
  347. return 0;
  348. }
  349. if (gpio_is_valid(info->gpio_nstby))
  350. gpio_set_value(info->gpio_nstby, 0);
  351. if (gpio_is_valid(info->gpio_nreset))
  352. gpio_set_value(info->gpio_nreset, 0);
  353. ret = regulator_bulk_enable(NOON010_NUM_SUPPLIES, info->supply);
  354. if (ret)
  355. return ret;
  356. if (gpio_is_valid(info->gpio_nreset)) {
  357. msleep(50);
  358. gpio_set_value(info->gpio_nreset, 1);
  359. }
  360. if (gpio_is_valid(info->gpio_nstby)) {
  361. udelay(1000);
  362. gpio_set_value(info->gpio_nstby, 1);
  363. }
  364. if (gpio_is_valid(info->gpio_nreset)) {
  365. udelay(1000);
  366. gpio_set_value(info->gpio_nreset, 0);
  367. msleep(100);
  368. gpio_set_value(info->gpio_nreset, 1);
  369. msleep(20);
  370. }
  371. info->power = 1;
  372. v4l2_dbg(1, debug, &info->sd, "%s: sensor is on\n", __func__);
  373. return 0;
  374. }
  375. static int power_disable(struct noon010_info *info)
  376. {
  377. int ret;
  378. if (!info->power) {
  379. v4l2_info(&info->sd, "%s: sensor is already off\n", __func__);
  380. return 0;
  381. }
  382. ret = regulator_bulk_disable(NOON010_NUM_SUPPLIES, info->supply);
  383. if (ret)
  384. return ret;
  385. if (gpio_is_valid(info->gpio_nstby))
  386. gpio_set_value(info->gpio_nstby, 0);
  387. if (gpio_is_valid(info->gpio_nreset))
  388. gpio_set_value(info->gpio_nreset, 0);
  389. info->power = 0;
  390. v4l2_dbg(1, debug, &info->sd, "%s: sensor is off\n", __func__);
  391. return 0;
  392. }
  393. static int noon010_s_ctrl(struct v4l2_ctrl *ctrl)
  394. {
  395. struct v4l2_subdev *sd = to_sd(ctrl);
  396. v4l2_dbg(1, debug, sd, "%s: ctrl_id: %d, value: %d\n",
  397. __func__, ctrl->id, ctrl->val);
  398. switch (ctrl->id) {
  399. case V4L2_CID_AUTO_WHITE_BALANCE:
  400. return noon010_enable_autowhitebalance(sd, ctrl->val);
  401. case V4L2_CID_BLUE_BALANCE:
  402. return cam_i2c_write(sd, MWB_BGAIN_REG, ctrl->val);
  403. case V4L2_CID_RED_BALANCE:
  404. return cam_i2c_write(sd, MWB_RGAIN_REG, ctrl->val);
  405. default:
  406. return -EINVAL;
  407. }
  408. }
  409. static int noon010_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  410. enum v4l2_mbus_pixelcode *code)
  411. {
  412. if (!code || index >= ARRAY_SIZE(noon010_formats))
  413. return -EINVAL;
  414. *code = noon010_formats[index].code;
  415. return 0;
  416. }
  417. static int noon010_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
  418. {
  419. struct noon010_info *info = to_noon010(sd);
  420. int ret;
  421. if (!mf)
  422. return -EINVAL;
  423. if (!info->curr_win || !info->curr_fmt) {
  424. ret = noon010_set_params(sd);
  425. if (ret)
  426. return ret;
  427. }
  428. mf->width = info->curr_win->width;
  429. mf->height = info->curr_win->height;
  430. mf->code = info->curr_fmt->code;
  431. mf->colorspace = info->curr_fmt->colorspace;
  432. mf->field = V4L2_FIELD_NONE;
  433. return 0;
  434. }
  435. /* Return nearest media bus frame format. */
  436. static const struct noon010_format *try_fmt(struct v4l2_subdev *sd,
  437. struct v4l2_mbus_framefmt *mf)
  438. {
  439. int i = ARRAY_SIZE(noon010_formats);
  440. noon010_try_frame_size(mf);
  441. while (i--)
  442. if (mf->code == noon010_formats[i].code)
  443. break;
  444. mf->code = noon010_formats[i].code;
  445. return &noon010_formats[i];
  446. }
  447. static int noon010_try_fmt(struct v4l2_subdev *sd,
  448. struct v4l2_mbus_framefmt *mf)
  449. {
  450. if (!sd || !mf)
  451. return -EINVAL;
  452. try_fmt(sd, mf);
  453. return 0;
  454. }
  455. static int noon010_s_fmt(struct v4l2_subdev *sd,
  456. struct v4l2_mbus_framefmt *mf)
  457. {
  458. struct noon010_info *info = to_noon010(sd);
  459. if (!sd || !mf)
  460. return -EINVAL;
  461. info->curr_fmt = try_fmt(sd, mf);
  462. return noon010_set_params(sd);
  463. }
  464. static int noon010_base_config(struct v4l2_subdev *sd)
  465. {
  466. struct noon010_info *info = to_noon010(sd);
  467. int ret;
  468. ret = noon010_bulk_write_reg(sd, noon010_base_regs);
  469. if (!ret) {
  470. info->curr_fmt = &noon010_formats[0];
  471. info->curr_win = &noon010_sizes[0];
  472. ret = noon010_set_params(sd);
  473. }
  474. if (!ret)
  475. ret = noon010_set_flip(sd, 1, 0);
  476. if (!ret)
  477. ret = noon010_power_ctrl(sd, false, false);
  478. /* sync the handler and the registers state */
  479. v4l2_ctrl_handler_setup(&to_noon010(sd)->hdl);
  480. return ret;
  481. }
  482. static int noon010_s_power(struct v4l2_subdev *sd, int on)
  483. {
  484. struct noon010_info *info = to_noon010(sd);
  485. const struct noon010pc30_platform_data *pdata = info->pdata;
  486. int ret = 0;
  487. if (WARN(pdata == NULL, "No platform data!\n"))
  488. return -ENOMEM;
  489. if (on) {
  490. ret = power_enable(info);
  491. if (ret)
  492. return ret;
  493. ret = noon010_base_config(sd);
  494. } else {
  495. noon010_power_ctrl(sd, false, true);
  496. ret = power_disable(info);
  497. info->curr_win = NULL;
  498. info->curr_fmt = NULL;
  499. }
  500. return ret;
  501. }
  502. static int noon010_g_chip_ident(struct v4l2_subdev *sd,
  503. struct v4l2_dbg_chip_ident *chip)
  504. {
  505. struct i2c_client *client = v4l2_get_subdevdata(sd);
  506. return v4l2_chip_ident_i2c_client(client, chip,
  507. V4L2_IDENT_NOON010PC30, 0);
  508. }
  509. static int noon010_log_status(struct v4l2_subdev *sd)
  510. {
  511. struct noon010_info *info = to_noon010(sd);
  512. v4l2_ctrl_handler_log_status(&info->hdl, sd->name);
  513. return 0;
  514. }
  515. static const struct v4l2_ctrl_ops noon010_ctrl_ops = {
  516. .s_ctrl = noon010_s_ctrl,
  517. };
  518. static const struct v4l2_subdev_core_ops noon010_core_ops = {
  519. .g_chip_ident = noon010_g_chip_ident,
  520. .s_power = noon010_s_power,
  521. .g_ctrl = v4l2_subdev_g_ctrl,
  522. .s_ctrl = v4l2_subdev_s_ctrl,
  523. .queryctrl = v4l2_subdev_queryctrl,
  524. .querymenu = v4l2_subdev_querymenu,
  525. .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
  526. .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
  527. .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
  528. .log_status = noon010_log_status,
  529. };
  530. static const struct v4l2_subdev_video_ops noon010_video_ops = {
  531. .g_mbus_fmt = noon010_g_fmt,
  532. .s_mbus_fmt = noon010_s_fmt,
  533. .try_mbus_fmt = noon010_try_fmt,
  534. .enum_mbus_fmt = noon010_enum_fmt,
  535. };
  536. static const struct v4l2_subdev_ops noon010_ops = {
  537. .core = &noon010_core_ops,
  538. .video = &noon010_video_ops,
  539. };
  540. /* Return 0 if NOON010PC30L sensor type was detected or -ENODEV otherwise. */
  541. static int noon010_detect(struct i2c_client *client, struct noon010_info *info)
  542. {
  543. int ret;
  544. ret = power_enable(info);
  545. if (ret)
  546. return ret;
  547. ret = i2c_smbus_read_byte_data(client, DEVICE_ID_REG);
  548. if (ret < 0)
  549. dev_err(&client->dev, "I2C read failed: 0x%X\n", ret);
  550. power_disable(info);
  551. return ret == NOON010PC30_ID ? 0 : -ENODEV;
  552. }
  553. static int noon010_probe(struct i2c_client *client,
  554. const struct i2c_device_id *id)
  555. {
  556. struct noon010_info *info;
  557. struct v4l2_subdev *sd;
  558. const struct noon010pc30_platform_data *pdata
  559. = client->dev.platform_data;
  560. int ret;
  561. int i;
  562. if (!pdata) {
  563. dev_err(&client->dev, "No platform data!\n");
  564. return -EIO;
  565. }
  566. info = kzalloc(sizeof(*info), GFP_KERNEL);
  567. if (!info)
  568. return -ENOMEM;
  569. sd = &info->sd;
  570. strlcpy(sd->name, MODULE_NAME, sizeof(sd->name));
  571. v4l2_i2c_subdev_init(sd, client, &noon010_ops);
  572. v4l2_ctrl_handler_init(&info->hdl, 3);
  573. v4l2_ctrl_new_std(&info->hdl, &noon010_ctrl_ops,
  574. V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
  575. v4l2_ctrl_new_std(&info->hdl, &noon010_ctrl_ops,
  576. V4L2_CID_RED_BALANCE, 0, 127, 1, 64);
  577. v4l2_ctrl_new_std(&info->hdl, &noon010_ctrl_ops,
  578. V4L2_CID_BLUE_BALANCE, 0, 127, 1, 64);
  579. sd->ctrl_handler = &info->hdl;
  580. ret = info->hdl.error;
  581. if (ret)
  582. goto np_err;
  583. info->pdata = client->dev.platform_data;
  584. info->i2c_reg_page = -1;
  585. info->gpio_nreset = -EINVAL;
  586. info->gpio_nstby = -EINVAL;
  587. if (gpio_is_valid(pdata->gpio_nreset)) {
  588. ret = gpio_request(pdata->gpio_nreset, "NOON010PC30 NRST");
  589. if (ret) {
  590. dev_err(&client->dev, "GPIO request error: %d\n", ret);
  591. goto np_err;
  592. }
  593. info->gpio_nreset = pdata->gpio_nreset;
  594. gpio_direction_output(info->gpio_nreset, 0);
  595. gpio_export(info->gpio_nreset, 0);
  596. }
  597. if (gpio_is_valid(pdata->gpio_nstby)) {
  598. ret = gpio_request(pdata->gpio_nstby, "NOON010PC30 NSTBY");
  599. if (ret) {
  600. dev_err(&client->dev, "GPIO request error: %d\n", ret);
  601. goto np_gpio_err;
  602. }
  603. info->gpio_nstby = pdata->gpio_nstby;
  604. gpio_direction_output(info->gpio_nstby, 0);
  605. gpio_export(info->gpio_nstby, 0);
  606. }
  607. for (i = 0; i < NOON010_NUM_SUPPLIES; i++)
  608. info->supply[i].supply = noon010_supply_name[i];
  609. ret = regulator_bulk_get(&client->dev, NOON010_NUM_SUPPLIES,
  610. info->supply);
  611. if (ret)
  612. goto np_reg_err;
  613. ret = noon010_detect(client, info);
  614. if (!ret)
  615. return 0;
  616. /* the sensor detection failed */
  617. regulator_bulk_free(NOON010_NUM_SUPPLIES, info->supply);
  618. np_reg_err:
  619. if (gpio_is_valid(info->gpio_nstby))
  620. gpio_free(info->gpio_nstby);
  621. np_gpio_err:
  622. if (gpio_is_valid(info->gpio_nreset))
  623. gpio_free(info->gpio_nreset);
  624. np_err:
  625. v4l2_ctrl_handler_free(&info->hdl);
  626. v4l2_device_unregister_subdev(sd);
  627. kfree(info);
  628. return ret;
  629. }
  630. static int noon010_remove(struct i2c_client *client)
  631. {
  632. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  633. struct noon010_info *info = to_noon010(sd);
  634. v4l2_device_unregister_subdev(sd);
  635. v4l2_ctrl_handler_free(&info->hdl);
  636. regulator_bulk_free(NOON010_NUM_SUPPLIES, info->supply);
  637. if (gpio_is_valid(info->gpio_nreset))
  638. gpio_free(info->gpio_nreset);
  639. if (gpio_is_valid(info->gpio_nstby))
  640. gpio_free(info->gpio_nstby);
  641. kfree(info);
  642. return 0;
  643. }
  644. static const struct i2c_device_id noon010_id[] = {
  645. { MODULE_NAME, 0 },
  646. { },
  647. };
  648. MODULE_DEVICE_TABLE(i2c, noon010_id);
  649. static struct i2c_driver noon010_i2c_driver = {
  650. .driver = {
  651. .name = MODULE_NAME
  652. },
  653. .probe = noon010_probe,
  654. .remove = noon010_remove,
  655. .id_table = noon010_id,
  656. };
  657. static int __init noon010_init(void)
  658. {
  659. return i2c_add_driver(&noon010_i2c_driver);
  660. }
  661. static void __exit noon010_exit(void)
  662. {
  663. i2c_del_driver(&noon010_i2c_driver);
  664. }
  665. module_init(noon010_init);
  666. module_exit(noon010_exit);
  667. MODULE_DESCRIPTION("Siliconfile NOON010PC30 camera driver");
  668. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  669. MODULE_LICENSE("GPL");