noon010pc30.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for SiliconFile NOON010PC30 CIF (1/11") Image Sensor with ISP
  4. *
  5. * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd.
  6. * Contact: Sylwester Nawrocki, <[email protected]>
  7. *
  8. * Initial register configuration based on a driver authored by
  9. * HeungJun Kim <[email protected]>.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/i2c.h>
  14. #include <linux/slab.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <media/i2c/noon010pc30.h>
  17. #include <linux/videodev2.h>
  18. #include <linux/module.h>
  19. #include <media/v4l2-ctrls.h>
  20. #include <media/v4l2-device.h>
  21. #include <media/v4l2-mediabus.h>
  22. #include <media/v4l2-subdev.h>
  23. static int debug;
  24. module_param(debug, int, 0644);
  25. MODULE_PARM_DESC(debug, "Enable module debug trace. Set to 1 to enable.");
  26. #define MODULE_NAME "NOON010PC30"
  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 VDO_CTL_REG(n) (0x0010 + (n))
  36. #define SYNC_CTL_REG 0x0012
  37. /* Window size and position */
  38. #define WIN_ROWH_REG 0x0013
  39. #define WIN_ROWL_REG 0x0014
  40. #define WIN_COLH_REG 0x0015
  41. #define WIN_COLL_REG 0x0016
  42. #define WIN_HEIGHTH_REG 0x0017
  43. #define WIN_HEIGHTL_REG 0x0018
  44. #define WIN_WIDTHH_REG 0x0019
  45. #define WIN_WIDTHL_REG 0x001A
  46. #define HBLANKH_REG 0x001B
  47. #define HBLANKL_REG 0x001C
  48. #define VSYNCH_REG 0x001D
  49. #define VSYNCL_REG 0x001E
  50. /* VSYNC control */
  51. #define VS_CTL_REG(n) (0x00A1 + (n))
  52. /* page 1 */
  53. #define ISP_CTL_REG(n) (0x0110 + (n))
  54. #define YOFS_REG 0x0119
  55. #define DARK_YOFS_REG 0x011A
  56. #define SAT_CTL_REG 0x0120
  57. #define BSAT_REG 0x0121
  58. #define RSAT_REG 0x0122
  59. /* Color correction */
  60. #define CMC_CTL_REG 0x0130
  61. #define CMC_OFSGH_REG 0x0133
  62. #define CMC_OFSGL_REG 0x0135
  63. #define CMC_SIGN_REG 0x0136
  64. #define CMC_GOFS_REG 0x0137
  65. #define CMC_COEF_REG(n) (0x0138 + (n))
  66. #define CMC_OFS_REG(n) (0x0141 + (n))
  67. /* Gamma correction */
  68. #define GMA_CTL_REG 0x0160
  69. #define GMA_COEF_REG(n) (0x0161 + (n))
  70. /* Lens Shading */
  71. #define LENS_CTRL_REG 0x01D0
  72. #define LENS_XCEN_REG 0x01D1
  73. #define LENS_YCEN_REG 0x01D2
  74. #define LENS_RC_REG 0x01D3
  75. #define LENS_GC_REG 0x01D4
  76. #define LENS_BC_REG 0x01D5
  77. #define L_AGON_REG 0x01D6
  78. #define L_AGOFF_REG 0x01D7
  79. /* Page 3 - Auto Exposure */
  80. #define AE_CTL_REG(n) (0x0310 + (n))
  81. #define AE_CTL9_REG 0x032C
  82. #define AE_CTL10_REG 0x032D
  83. #define AE_YLVL_REG 0x031C
  84. #define AE_YTH_REG(n) (0x031D + (n))
  85. #define AE_WGT_REG 0x0326
  86. #define EXP_TIMEH_REG 0x0333
  87. #define EXP_TIMEM_REG 0x0334
  88. #define EXP_TIMEL_REG 0x0335
  89. #define EXP_MMINH_REG 0x0336
  90. #define EXP_MMINL_REG 0x0337
  91. #define EXP_MMAXH_REG 0x0338
  92. #define EXP_MMAXM_REG 0x0339
  93. #define EXP_MMAXL_REG 0x033A
  94. /* Page 4 - Auto White Balance */
  95. #define AWB_CTL_REG(n) (0x0410 + (n))
  96. #define AWB_ENABE 0x80
  97. #define AWB_WGHT_REG 0x0419
  98. #define BGAIN_PAR_REG(n) (0x044F + (n))
  99. /* Manual white balance, when AWB_CTL2[0]=1 */
  100. #define MWB_RGAIN_REG 0x0466
  101. #define MWB_BGAIN_REG 0x0467
  102. /* The token to mark an array end */
  103. #define REG_TERM 0xFFFF
  104. struct noon010_format {
  105. u32 code;
  106. enum v4l2_colorspace colorspace;
  107. u16 ispctl1_reg;
  108. };
  109. struct noon010_frmsize {
  110. u16 width;
  111. u16 height;
  112. int vid_ctl1;
  113. };
  114. static const char * const noon010_supply_name[] = {
  115. "vdd_core", "vddio", "vdda"
  116. };
  117. #define NOON010_NUM_SUPPLIES ARRAY_SIZE(noon010_supply_name)
  118. struct noon010_info {
  119. struct v4l2_subdev sd;
  120. struct media_pad pad;
  121. struct v4l2_ctrl_handler hdl;
  122. struct regulator_bulk_data supply[NOON010_NUM_SUPPLIES];
  123. struct gpio_desc *reset;
  124. struct gpio_desc *stby;
  125. /* Protects the struct members below */
  126. struct mutex lock;
  127. const struct noon010_format *curr_fmt;
  128. const struct noon010_frmsize *curr_win;
  129. unsigned int apply_new_cfg:1;
  130. unsigned int streaming:1;
  131. unsigned int hflip:1;
  132. unsigned int vflip:1;
  133. unsigned int power:1;
  134. u8 i2c_reg_page;
  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 = MEDIA_BUS_FMT_YUYV8_2X8,
  160. .colorspace = V4L2_COLORSPACE_JPEG,
  161. .ispctl1_reg = 0x03,
  162. }, {
  163. .code = MEDIA_BUS_FMT_YVYU8_2X8,
  164. .colorspace = V4L2_COLORSPACE_JPEG,
  165. .ispctl1_reg = 0x02,
  166. }, {
  167. .code = MEDIA_BUS_FMT_VYUY8_2X8,
  168. .colorspace = V4L2_COLORSPACE_JPEG,
  169. .ispctl1_reg = 0,
  170. }, {
  171. .code = MEDIA_BUS_FMT_UYVY8_2X8,
  172. .colorspace = V4L2_COLORSPACE_JPEG,
  173. .ispctl1_reg = 0x01,
  174. }, {
  175. .code = MEDIA_BUS_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. udelay(20);
  272. }
  273. if (!ret) {
  274. ret = cam_i2c_write(sd, POWER_CTRL_REG, reg);
  275. if (reset && !ret)
  276. info->i2c_reg_page = -1;
  277. }
  278. return ret;
  279. }
  280. /* Automatic white balance control */
  281. static int noon010_enable_autowhitebalance(struct v4l2_subdev *sd, int on)
  282. {
  283. int ret;
  284. ret = cam_i2c_write(sd, AWB_CTL_REG(1), on ? 0x2E : 0x2F);
  285. if (!ret)
  286. ret = cam_i2c_write(sd, AWB_CTL_REG(0), on ? 0xFB : 0x7B);
  287. return ret;
  288. }
  289. /* Called with struct noon010_info.lock mutex held */
  290. static int noon010_set_flip(struct v4l2_subdev *sd, int hflip, int vflip)
  291. {
  292. struct noon010_info *info = to_noon010(sd);
  293. int reg, ret;
  294. reg = cam_i2c_read(sd, VDO_CTL_REG(1));
  295. if (reg < 0)
  296. return reg;
  297. reg &= 0x7C;
  298. if (hflip)
  299. reg |= 0x01;
  300. if (vflip)
  301. reg |= 0x02;
  302. ret = cam_i2c_write(sd, VDO_CTL_REG(1), reg | 0x80);
  303. if (!ret) {
  304. info->hflip = hflip;
  305. info->vflip = vflip;
  306. }
  307. return ret;
  308. }
  309. /* Configure resolution and color format */
  310. static int noon010_set_params(struct v4l2_subdev *sd)
  311. {
  312. struct noon010_info *info = to_noon010(sd);
  313. int ret = cam_i2c_write(sd, VDO_CTL_REG(0),
  314. info->curr_win->vid_ctl1);
  315. if (ret)
  316. return ret;
  317. return cam_i2c_write(sd, ISP_CTL_REG(0),
  318. info->curr_fmt->ispctl1_reg);
  319. }
  320. /* Find nearest matching image pixel size. */
  321. static int noon010_try_frame_size(struct v4l2_mbus_framefmt *mf,
  322. const struct noon010_frmsize **size)
  323. {
  324. unsigned int min_err = ~0;
  325. int i = ARRAY_SIZE(noon010_sizes);
  326. const struct noon010_frmsize *fsize = &noon010_sizes[0],
  327. *match = NULL;
  328. while (i--) {
  329. int err = abs(fsize->width - mf->width)
  330. + abs(fsize->height - mf->height);
  331. if (err < min_err) {
  332. min_err = err;
  333. match = fsize;
  334. }
  335. fsize++;
  336. }
  337. if (match) {
  338. mf->width = match->width;
  339. mf->height = match->height;
  340. if (size)
  341. *size = match;
  342. return 0;
  343. }
  344. return -EINVAL;
  345. }
  346. /* Called with info.lock mutex held */
  347. static int power_enable(struct noon010_info *info)
  348. {
  349. int ret;
  350. if (info->power) {
  351. v4l2_info(&info->sd, "%s: sensor is already on\n", __func__);
  352. return 0;
  353. }
  354. /* Assert standby: line should be flagged active low in descriptor */
  355. if (info->stby)
  356. gpiod_set_value(info->stby, 1);
  357. /* Assert reset: line should be flagged active low in descriptor */
  358. if (info->reset)
  359. gpiod_set_value(info->reset, 1);
  360. ret = regulator_bulk_enable(NOON010_NUM_SUPPLIES, info->supply);
  361. if (ret)
  362. return ret;
  363. /* De-assert reset and standby */
  364. if (info->reset) {
  365. msleep(50);
  366. gpiod_set_value(info->reset, 0);
  367. }
  368. if (info->stby) {
  369. udelay(1000);
  370. gpiod_set_value(info->stby, 0);
  371. }
  372. /* Cycle reset: assert and deassert */
  373. if (info->reset) {
  374. udelay(1000);
  375. gpiod_set_value(info->reset, 1);
  376. msleep(100);
  377. gpiod_set_value(info->reset, 0);
  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. /* Assert standby and reset */
  396. if (info->stby)
  397. gpiod_set_value(info->stby, 1);
  398. if (info->reset)
  399. gpiod_set_value(info->reset, 1);
  400. info->power = 0;
  401. v4l2_dbg(1, debug, &info->sd, "%s: sensor is off\n", __func__);
  402. return 0;
  403. }
  404. static int noon010_s_ctrl(struct v4l2_ctrl *ctrl)
  405. {
  406. struct v4l2_subdev *sd = to_sd(ctrl);
  407. struct noon010_info *info = to_noon010(sd);
  408. int ret = 0;
  409. v4l2_dbg(1, debug, sd, "%s: ctrl_id: %d, value: %d\n",
  410. __func__, ctrl->id, ctrl->val);
  411. mutex_lock(&info->lock);
  412. /*
  413. * If the device is not powered up by the host driver do
  414. * not apply any controls to H/W at this time. Instead
  415. * the controls will be restored right after power-up.
  416. */
  417. if (!info->power)
  418. goto unlock;
  419. switch (ctrl->id) {
  420. case V4L2_CID_AUTO_WHITE_BALANCE:
  421. ret = noon010_enable_autowhitebalance(sd, ctrl->val);
  422. break;
  423. case V4L2_CID_BLUE_BALANCE:
  424. ret = cam_i2c_write(sd, MWB_BGAIN_REG, ctrl->val);
  425. break;
  426. case V4L2_CID_RED_BALANCE:
  427. ret = cam_i2c_write(sd, MWB_RGAIN_REG, ctrl->val);
  428. break;
  429. default:
  430. ret = -EINVAL;
  431. }
  432. unlock:
  433. mutex_unlock(&info->lock);
  434. return ret;
  435. }
  436. static int noon010_enum_mbus_code(struct v4l2_subdev *sd,
  437. struct v4l2_subdev_state *sd_state,
  438. struct v4l2_subdev_mbus_code_enum *code)
  439. {
  440. if (code->index >= ARRAY_SIZE(noon010_formats))
  441. return -EINVAL;
  442. code->code = noon010_formats[code->index].code;
  443. return 0;
  444. }
  445. static int noon010_get_fmt(struct v4l2_subdev *sd,
  446. struct v4l2_subdev_state *sd_state,
  447. struct v4l2_subdev_format *fmt)
  448. {
  449. struct noon010_info *info = to_noon010(sd);
  450. struct v4l2_mbus_framefmt *mf;
  451. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  452. if (sd_state) {
  453. mf = v4l2_subdev_get_try_format(sd, sd_state, 0);
  454. fmt->format = *mf;
  455. }
  456. return 0;
  457. }
  458. mf = &fmt->format;
  459. mutex_lock(&info->lock);
  460. mf->width = info->curr_win->width;
  461. mf->height = info->curr_win->height;
  462. mf->code = info->curr_fmt->code;
  463. mf->colorspace = info->curr_fmt->colorspace;
  464. mf->field = V4L2_FIELD_NONE;
  465. mutex_unlock(&info->lock);
  466. return 0;
  467. }
  468. /* Return nearest media bus frame format. */
  469. static const struct noon010_format *noon010_try_fmt(struct v4l2_subdev *sd,
  470. struct v4l2_mbus_framefmt *mf)
  471. {
  472. int i = ARRAY_SIZE(noon010_formats);
  473. while (--i)
  474. if (mf->code == noon010_formats[i].code)
  475. break;
  476. mf->code = noon010_formats[i].code;
  477. return &noon010_formats[i];
  478. }
  479. static int noon010_set_fmt(struct v4l2_subdev *sd,
  480. struct v4l2_subdev_state *sd_state,
  481. struct v4l2_subdev_format *fmt)
  482. {
  483. struct noon010_info *info = to_noon010(sd);
  484. const struct noon010_frmsize *size = NULL;
  485. const struct noon010_format *nf;
  486. struct v4l2_mbus_framefmt *mf;
  487. int ret = 0;
  488. nf = noon010_try_fmt(sd, &fmt->format);
  489. noon010_try_frame_size(&fmt->format, &size);
  490. fmt->format.colorspace = V4L2_COLORSPACE_JPEG;
  491. fmt->format.field = V4L2_FIELD_NONE;
  492. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  493. if (sd_state) {
  494. mf = v4l2_subdev_get_try_format(sd, sd_state, 0);
  495. *mf = fmt->format;
  496. }
  497. return 0;
  498. }
  499. mutex_lock(&info->lock);
  500. if (!info->streaming) {
  501. info->apply_new_cfg = 1;
  502. info->curr_fmt = nf;
  503. info->curr_win = size;
  504. } else {
  505. ret = -EBUSY;
  506. }
  507. mutex_unlock(&info->lock);
  508. return ret;
  509. }
  510. /* Called with struct noon010_info.lock mutex held */
  511. static int noon010_base_config(struct v4l2_subdev *sd)
  512. {
  513. int ret = noon010_bulk_write_reg(sd, noon010_base_regs);
  514. if (!ret)
  515. ret = noon010_set_params(sd);
  516. if (!ret)
  517. ret = noon010_set_flip(sd, 1, 0);
  518. return ret;
  519. }
  520. static int noon010_s_power(struct v4l2_subdev *sd, int on)
  521. {
  522. struct noon010_info *info = to_noon010(sd);
  523. int ret;
  524. mutex_lock(&info->lock);
  525. if (on) {
  526. ret = power_enable(info);
  527. if (!ret)
  528. ret = noon010_base_config(sd);
  529. } else {
  530. noon010_power_ctrl(sd, false, true);
  531. ret = power_disable(info);
  532. }
  533. mutex_unlock(&info->lock);
  534. /* Restore the controls state */
  535. if (!ret && on)
  536. ret = v4l2_ctrl_handler_setup(&info->hdl);
  537. return ret;
  538. }
  539. static int noon010_s_stream(struct v4l2_subdev *sd, int on)
  540. {
  541. struct noon010_info *info = to_noon010(sd);
  542. int ret = 0;
  543. mutex_lock(&info->lock);
  544. if (!info->streaming != !on) {
  545. ret = noon010_power_ctrl(sd, false, !on);
  546. if (!ret)
  547. info->streaming = on;
  548. }
  549. if (!ret && on && info->apply_new_cfg) {
  550. ret = noon010_set_params(sd);
  551. if (!ret)
  552. info->apply_new_cfg = 0;
  553. }
  554. mutex_unlock(&info->lock);
  555. return ret;
  556. }
  557. static int noon010_log_status(struct v4l2_subdev *sd)
  558. {
  559. struct noon010_info *info = to_noon010(sd);
  560. v4l2_ctrl_handler_log_status(&info->hdl, sd->name);
  561. return 0;
  562. }
  563. static int noon010_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  564. {
  565. struct v4l2_mbus_framefmt *mf = v4l2_subdev_get_try_format(sd,
  566. fh->state,
  567. 0);
  568. mf->width = noon010_sizes[0].width;
  569. mf->height = noon010_sizes[0].height;
  570. mf->code = noon010_formats[0].code;
  571. mf->colorspace = V4L2_COLORSPACE_JPEG;
  572. mf->field = V4L2_FIELD_NONE;
  573. return 0;
  574. }
  575. static const struct v4l2_subdev_internal_ops noon010_subdev_internal_ops = {
  576. .open = noon010_open,
  577. };
  578. static const struct v4l2_ctrl_ops noon010_ctrl_ops = {
  579. .s_ctrl = noon010_s_ctrl,
  580. };
  581. static const struct v4l2_subdev_core_ops noon010_core_ops = {
  582. .s_power = noon010_s_power,
  583. .log_status = noon010_log_status,
  584. };
  585. static const struct v4l2_subdev_pad_ops noon010_pad_ops = {
  586. .enum_mbus_code = noon010_enum_mbus_code,
  587. .get_fmt = noon010_get_fmt,
  588. .set_fmt = noon010_set_fmt,
  589. };
  590. static const struct v4l2_subdev_video_ops noon010_video_ops = {
  591. .s_stream = noon010_s_stream,
  592. };
  593. static const struct v4l2_subdev_ops noon010_ops = {
  594. .core = &noon010_core_ops,
  595. .pad = &noon010_pad_ops,
  596. .video = &noon010_video_ops,
  597. };
  598. /* Return 0 if NOON010PC30L sensor type was detected or -ENODEV otherwise. */
  599. static int noon010_detect(struct i2c_client *client, struct noon010_info *info)
  600. {
  601. int ret;
  602. ret = power_enable(info);
  603. if (ret)
  604. return ret;
  605. ret = i2c_smbus_read_byte_data(client, DEVICE_ID_REG);
  606. if (ret < 0)
  607. dev_err(&client->dev, "I2C read failed: 0x%X\n", ret);
  608. power_disable(info);
  609. return ret == NOON010PC30_ID ? 0 : -ENODEV;
  610. }
  611. static int noon010_probe(struct i2c_client *client,
  612. const struct i2c_device_id *id)
  613. {
  614. struct noon010_info *info;
  615. struct v4l2_subdev *sd;
  616. const struct noon010pc30_platform_data *pdata
  617. = client->dev.platform_data;
  618. int ret;
  619. int i;
  620. if (!pdata) {
  621. dev_err(&client->dev, "No platform data!\n");
  622. return -EIO;
  623. }
  624. info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
  625. if (!info)
  626. return -ENOMEM;
  627. mutex_init(&info->lock);
  628. sd = &info->sd;
  629. v4l2_i2c_subdev_init(sd, client, &noon010_ops);
  630. /* Static name; NEVER use in new drivers! */
  631. strscpy(sd->name, MODULE_NAME, sizeof(sd->name));
  632. sd->internal_ops = &noon010_subdev_internal_ops;
  633. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  634. v4l2_ctrl_handler_init(&info->hdl, 3);
  635. v4l2_ctrl_new_std(&info->hdl, &noon010_ctrl_ops,
  636. V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
  637. v4l2_ctrl_new_std(&info->hdl, &noon010_ctrl_ops,
  638. V4L2_CID_RED_BALANCE, 0, 127, 1, 64);
  639. v4l2_ctrl_new_std(&info->hdl, &noon010_ctrl_ops,
  640. V4L2_CID_BLUE_BALANCE, 0, 127, 1, 64);
  641. sd->ctrl_handler = &info->hdl;
  642. ret = info->hdl.error;
  643. if (ret)
  644. goto np_err;
  645. info->i2c_reg_page = -1;
  646. info->curr_fmt = &noon010_formats[0];
  647. info->curr_win = &noon010_sizes[0];
  648. /* Request reset asserted so we get put into reset */
  649. info->reset = devm_gpiod_get(&client->dev, "reset", GPIOD_OUT_HIGH);
  650. if (IS_ERR(info->reset)) {
  651. ret = PTR_ERR(info->reset);
  652. goto np_err;
  653. }
  654. gpiod_set_consumer_name(info->reset, "NOON010PC30 NRST");
  655. /* Request standby asserted so we get put into standby */
  656. info->stby = devm_gpiod_get(&client->dev, "standby", GPIOD_OUT_HIGH);
  657. if (IS_ERR(info->stby)) {
  658. ret = PTR_ERR(info->stby);
  659. goto np_err;
  660. }
  661. gpiod_set_consumer_name(info->reset, "NOON010PC30 STBY");
  662. for (i = 0; i < NOON010_NUM_SUPPLIES; i++)
  663. info->supply[i].supply = noon010_supply_name[i];
  664. ret = devm_regulator_bulk_get(&client->dev, NOON010_NUM_SUPPLIES,
  665. info->supply);
  666. if (ret)
  667. goto np_err;
  668. info->pad.flags = MEDIA_PAD_FL_SOURCE;
  669. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  670. ret = media_entity_pads_init(&sd->entity, 1, &info->pad);
  671. if (ret < 0)
  672. goto np_err;
  673. ret = noon010_detect(client, info);
  674. if (!ret)
  675. return 0;
  676. np_err:
  677. v4l2_ctrl_handler_free(&info->hdl);
  678. v4l2_device_unregister_subdev(sd);
  679. return ret;
  680. }
  681. static void noon010_remove(struct i2c_client *client)
  682. {
  683. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  684. struct noon010_info *info = to_noon010(sd);
  685. v4l2_device_unregister_subdev(sd);
  686. v4l2_ctrl_handler_free(&info->hdl);
  687. media_entity_cleanup(&sd->entity);
  688. }
  689. static const struct i2c_device_id noon010_id[] = {
  690. { MODULE_NAME, 0 },
  691. { },
  692. };
  693. MODULE_DEVICE_TABLE(i2c, noon010_id);
  694. static struct i2c_driver noon010_i2c_driver = {
  695. .driver = {
  696. .name = MODULE_NAME
  697. },
  698. .probe = noon010_probe,
  699. .remove = noon010_remove,
  700. .id_table = noon010_id,
  701. };
  702. module_i2c_driver(noon010_i2c_driver);
  703. MODULE_DESCRIPTION("Siliconfile NOON010PC30 camera driver");
  704. MODULE_AUTHOR("Sylwester Nawrocki <[email protected]>");
  705. MODULE_LICENSE("GPL");