s5k4ecgx.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /*
  2. * Driver for Samsung S5K4ECGX 1/4" 5Mp CMOS Image Sensor SoC
  3. * with an Embedded Image Signal Processor.
  4. *
  5. * Copyright (C) 2012, Linaro, Sangwook Lee <sangwook.lee@linaro.org>
  6. * Copyright (C) 2012, Insignal Co,. Ltd, Homin Lee <suapapa@insignal.co.kr>
  7. *
  8. * Based on s5k6aa and noon010pc30 driver
  9. * Copyright (C) 2011, Samsung Electronics Co., Ltd.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/crc32.h>
  18. #include <linux/ctype.h>
  19. #include <linux/delay.h>
  20. #include <linux/firmware.h>
  21. #include <linux/gpio.h>
  22. #include <linux/i2c.h>
  23. #include <linux/module.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/slab.h>
  26. #include <asm/unaligned.h>
  27. #include <media/media-entity.h>
  28. #include <media/s5k4ecgx.h>
  29. #include <media/v4l2-ctrls.h>
  30. #include <media/v4l2-device.h>
  31. #include <media/v4l2-mediabus.h>
  32. #include <media/v4l2-subdev.h>
  33. static int debug;
  34. module_param(debug, int, 0644);
  35. #define S5K4ECGX_DRIVER_NAME "s5k4ecgx"
  36. #define S5K4ECGX_FIRMWARE "s5k4ecgx.bin"
  37. /* Firmware revision information */
  38. #define REG_FW_REVISION 0x700001a6
  39. #define REG_FW_VERSION 0x700001a4
  40. #define S5K4ECGX_REVISION_1_1 0x11
  41. #define S5K4ECGX_FW_VERSION 0x4ec0
  42. /* General purpose parameters */
  43. #define REG_USER_BRIGHTNESS 0x7000022c
  44. #define REG_USER_CONTRAST 0x7000022e
  45. #define REG_USER_SATURATION 0x70000230
  46. #define REG_G_ENABLE_PREV 0x7000023e
  47. #define REG_G_ENABLE_PREV_CHG 0x70000240
  48. #define REG_G_NEW_CFG_SYNC 0x7000024a
  49. #define REG_G_PREV_IN_WIDTH 0x70000250
  50. #define REG_G_PREV_IN_HEIGHT 0x70000252
  51. #define REG_G_PREV_IN_XOFFS 0x70000254
  52. #define REG_G_PREV_IN_YOFFS 0x70000256
  53. #define REG_G_CAP_IN_WIDTH 0x70000258
  54. #define REG_G_CAP_IN_HEIGHT 0x7000025a
  55. #define REG_G_CAP_IN_XOFFS 0x7000025c
  56. #define REG_G_CAP_IN_YOFFS 0x7000025e
  57. #define REG_G_INPUTS_CHANGE_REQ 0x70000262
  58. #define REG_G_ACTIVE_PREV_CFG 0x70000266
  59. #define REG_G_PREV_CFG_CHG 0x70000268
  60. #define REG_G_PREV_OPEN_AFTER_CH 0x7000026a
  61. /* Preview context register sets. n = 0...4. */
  62. #define PREG(n, x) ((n) * 0x30 + (x))
  63. #define REG_P_OUT_WIDTH(n) PREG(n, 0x700002a6)
  64. #define REG_P_OUT_HEIGHT(n) PREG(n, 0x700002a8)
  65. #define REG_P_FMT(n) PREG(n, 0x700002aa)
  66. #define REG_P_PVI_MASK(n) PREG(n, 0x700002b4)
  67. #define REG_P_FR_TIME_TYPE(n) PREG(n, 0x700002be)
  68. #define FR_TIME_DYNAMIC 0
  69. #define FR_TIME_FIXED 1
  70. #define FR_TIME_FIXED_ACCURATE 2
  71. #define REG_P_FR_TIME_Q_TYPE(n) PREG(n, 0x700002c0)
  72. #define FR_TIME_Q_DYNAMIC 0
  73. #define FR_TIME_Q_BEST_FRRATE 1
  74. #define FR_TIME_Q_BEST_QUALITY 2
  75. /* Frame period in 0.1 ms units */
  76. #define REG_P_MAX_FR_TIME(n) PREG(n, 0x700002c2)
  77. #define REG_P_MIN_FR_TIME(n) PREG(n, 0x700002c4)
  78. #define US_TO_FR_TIME(__t) ((__t) / 100)
  79. #define REG_P_PREV_MIRROR(n) PREG(n, 0x700002d0)
  80. #define REG_P_CAP_MIRROR(n) PREG(n, 0x700002d2)
  81. #define REG_G_PREVZOOM_IN_WIDTH 0x70000494
  82. #define REG_G_PREVZOOM_IN_HEIGHT 0x70000496
  83. #define REG_G_PREVZOOM_IN_XOFFS 0x70000498
  84. #define REG_G_PREVZOOM_IN_YOFFS 0x7000049a
  85. #define REG_G_CAPZOOM_IN_WIDTH 0x7000049c
  86. #define REG_G_CAPZOOM_IN_HEIGHT 0x7000049e
  87. #define REG_G_CAPZOOM_IN_XOFFS 0x700004a0
  88. #define REG_G_CAPZOOM_IN_YOFFS 0x700004a2
  89. /* n = 0...4 */
  90. #define REG_USER_SHARPNESS(n) (0x70000a28 + (n) * 0xb6)
  91. /* Reduce sharpness range for user space API */
  92. #define SHARPNESS_DIV 8208
  93. #define TOK_TERM 0xffffffff
  94. /*
  95. * FIXME: This is copied from s5k6aa, because of no information
  96. * in the S5K4ECGX datasheet.
  97. * H/W register Interface (0xd0000000 - 0xd0000fff)
  98. */
  99. #define AHB_MSB_ADDR_PTR 0xfcfc
  100. #define GEN_REG_OFFSH 0xd000
  101. #define REG_CMDWR_ADDRH 0x0028
  102. #define REG_CMDWR_ADDRL 0x002a
  103. #define REG_CMDRD_ADDRH 0x002c
  104. #define REG_CMDRD_ADDRL 0x002e
  105. #define REG_CMDBUF0_ADDR 0x0f12
  106. struct s5k4ecgx_frmsize {
  107. struct v4l2_frmsize_discrete size;
  108. /* Fixed sensor matrix crop rectangle */
  109. struct v4l2_rect input_window;
  110. };
  111. struct regval_list {
  112. u32 addr;
  113. u16 val;
  114. };
  115. /*
  116. * TODO: currently only preview is supported and snapshot (capture)
  117. * is not implemented yet
  118. */
  119. static const struct s5k4ecgx_frmsize s5k4ecgx_prev_sizes[] = {
  120. {
  121. .size = { 176, 144 },
  122. .input_window = { 0x00, 0x00, 0x928, 0x780 },
  123. }, {
  124. .size = { 352, 288 },
  125. .input_window = { 0x00, 0x00, 0x928, 0x780 },
  126. }, {
  127. .size = { 640, 480 },
  128. .input_window = { 0x00, 0x00, 0xa00, 0x780 },
  129. }, {
  130. .size = { 720, 480 },
  131. .input_window = { 0x00, 0x00, 0xa00, 0x6a8 },
  132. }
  133. };
  134. #define S5K4ECGX_NUM_PREV ARRAY_SIZE(s5k4ecgx_prev_sizes)
  135. struct s5k4ecgx_pixfmt {
  136. u32 code;
  137. u32 colorspace;
  138. /* REG_TC_PCFG_Format register value */
  139. u16 reg_p_format;
  140. };
  141. /* By default value, output from sensor will be YUV422 0-255 */
  142. static const struct s5k4ecgx_pixfmt s5k4ecgx_formats[] = {
  143. { MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG, 5 },
  144. };
  145. static const char * const s5k4ecgx_supply_names[] = {
  146. /*
  147. * Usually 2.8V is used for analog power (vdda)
  148. * and digital IO (vddio, vdddcore)
  149. */
  150. "vdda",
  151. "vddio",
  152. "vddcore",
  153. "vddreg", /* The internal s5k4ecgx regulator's supply (1.8V) */
  154. };
  155. #define S5K4ECGX_NUM_SUPPLIES ARRAY_SIZE(s5k4ecgx_supply_names)
  156. enum s5k4ecgx_gpio_id {
  157. STBY,
  158. RST,
  159. GPIO_NUM,
  160. };
  161. struct s5k4ecgx {
  162. struct v4l2_subdev sd;
  163. struct media_pad pad;
  164. struct v4l2_ctrl_handler handler;
  165. struct s5k4ecgx_platform_data *pdata;
  166. const struct s5k4ecgx_pixfmt *curr_pixfmt;
  167. const struct s5k4ecgx_frmsize *curr_frmsize;
  168. struct mutex lock;
  169. u8 streaming;
  170. u8 set_params;
  171. struct regulator_bulk_data supplies[S5K4ECGX_NUM_SUPPLIES];
  172. struct s5k4ecgx_gpio gpio[GPIO_NUM];
  173. };
  174. static inline struct s5k4ecgx *to_s5k4ecgx(struct v4l2_subdev *sd)
  175. {
  176. return container_of(sd, struct s5k4ecgx, sd);
  177. }
  178. static int s5k4ecgx_i2c_read(struct i2c_client *client, u16 addr, u16 *val)
  179. {
  180. u8 wbuf[2] = { addr >> 8, addr & 0xff };
  181. struct i2c_msg msg[2];
  182. u8 rbuf[2];
  183. int ret;
  184. msg[0].addr = client->addr;
  185. msg[0].flags = 0;
  186. msg[0].len = 2;
  187. msg[0].buf = wbuf;
  188. msg[1].addr = client->addr;
  189. msg[1].flags = I2C_M_RD;
  190. msg[1].len = 2;
  191. msg[1].buf = rbuf;
  192. ret = i2c_transfer(client->adapter, msg, 2);
  193. *val = be16_to_cpu(*((__be16 *)rbuf));
  194. v4l2_dbg(4, debug, client, "i2c_read: 0x%04X : 0x%04x\n", addr, *val);
  195. return ret == 2 ? 0 : ret;
  196. }
  197. static int s5k4ecgx_i2c_write(struct i2c_client *client, u16 addr, u16 val)
  198. {
  199. u8 buf[4] = { addr >> 8, addr & 0xff, val >> 8, val & 0xff };
  200. int ret = i2c_master_send(client, buf, 4);
  201. v4l2_dbg(4, debug, client, "i2c_write: 0x%04x : 0x%04x\n", addr, val);
  202. return ret == 4 ? 0 : ret;
  203. }
  204. static int s5k4ecgx_write(struct i2c_client *client, u32 addr, u16 val)
  205. {
  206. u16 high = addr >> 16, low = addr & 0xffff;
  207. int ret;
  208. v4l2_dbg(3, debug, client, "write: 0x%08x : 0x%04x\n", addr, val);
  209. ret = s5k4ecgx_i2c_write(client, REG_CMDWR_ADDRH, high);
  210. if (!ret)
  211. ret = s5k4ecgx_i2c_write(client, REG_CMDWR_ADDRL, low);
  212. if (!ret)
  213. ret = s5k4ecgx_i2c_write(client, REG_CMDBUF0_ADDR, val);
  214. return ret;
  215. }
  216. static int s5k4ecgx_read(struct i2c_client *client, u32 addr, u16 *val)
  217. {
  218. u16 high = addr >> 16, low = addr & 0xffff;
  219. int ret;
  220. ret = s5k4ecgx_i2c_write(client, REG_CMDRD_ADDRH, high);
  221. if (!ret)
  222. ret = s5k4ecgx_i2c_write(client, REG_CMDRD_ADDRL, low);
  223. if (!ret)
  224. ret = s5k4ecgx_i2c_read(client, REG_CMDBUF0_ADDR, val);
  225. if (!ret)
  226. dev_err(&client->dev, "Failed to execute read command\n");
  227. return ret;
  228. }
  229. static int s5k4ecgx_read_fw_ver(struct v4l2_subdev *sd)
  230. {
  231. struct i2c_client *client = v4l2_get_subdevdata(sd);
  232. u16 hw_rev, fw_ver = 0;
  233. int ret;
  234. ret = s5k4ecgx_read(client, REG_FW_VERSION, &fw_ver);
  235. if (ret < 0 || fw_ver != S5K4ECGX_FW_VERSION) {
  236. v4l2_err(sd, "FW version check failed!\n");
  237. return -ENODEV;
  238. }
  239. ret = s5k4ecgx_read(client, REG_FW_REVISION, &hw_rev);
  240. if (ret < 0)
  241. return ret;
  242. v4l2_info(sd, "chip found FW ver: 0x%x, HW rev: 0x%x\n",
  243. fw_ver, hw_rev);
  244. return 0;
  245. }
  246. static int s5k4ecgx_set_ahb_address(struct v4l2_subdev *sd)
  247. {
  248. struct i2c_client *client = v4l2_get_subdevdata(sd);
  249. int ret;
  250. /* Set APB peripherals start address */
  251. ret = s5k4ecgx_i2c_write(client, AHB_MSB_ADDR_PTR, GEN_REG_OFFSH);
  252. if (ret < 0)
  253. return ret;
  254. /*
  255. * FIXME: This is copied from s5k6aa, because of no information
  256. * in s5k4ecgx's datasheet.
  257. * sw_reset is activated to put device into idle status
  258. */
  259. ret = s5k4ecgx_i2c_write(client, 0x0010, 0x0001);
  260. if (ret < 0)
  261. return ret;
  262. ret = s5k4ecgx_i2c_write(client, 0x1030, 0x0000);
  263. if (ret < 0)
  264. return ret;
  265. /* Halt ARM CPU */
  266. return s5k4ecgx_i2c_write(client, 0x0014, 0x0001);
  267. }
  268. #define FW_CRC_SIZE 4
  269. /* Register address, value are 4, 2 bytes */
  270. #define FW_RECORD_SIZE 6
  271. /*
  272. * The firmware has following format:
  273. * < total number of records (4 bytes + 2 bytes padding) N >,
  274. * < record 0 >, ..., < record N - 1 >, < CRC32-CCITT (4-bytes) >,
  275. * where "record" is a 4-byte register address followed by 2-byte
  276. * register value (little endian).
  277. * The firmware generator can be found in following git repository:
  278. * git://git.linaro.org/people/sangwook/fimc-v4l2-app.git
  279. */
  280. static int s5k4ecgx_load_firmware(struct v4l2_subdev *sd)
  281. {
  282. struct i2c_client *client = v4l2_get_subdevdata(sd);
  283. const struct firmware *fw;
  284. const u8 *ptr;
  285. int err, i, regs_num;
  286. u32 addr, crc, crc_file, addr_inc = 0;
  287. u16 val;
  288. err = request_firmware(&fw, S5K4ECGX_FIRMWARE, sd->v4l2_dev->dev);
  289. if (err) {
  290. v4l2_err(sd, "Failed to read firmware %s\n", S5K4ECGX_FIRMWARE);
  291. return err;
  292. }
  293. regs_num = get_unaligned_le32(fw->data);
  294. v4l2_dbg(3, debug, sd, "FW: %s size %zu register sets %d\n",
  295. S5K4ECGX_FIRMWARE, fw->size, regs_num);
  296. regs_num++; /* Add header */
  297. if (fw->size != regs_num * FW_RECORD_SIZE + FW_CRC_SIZE) {
  298. err = -EINVAL;
  299. goto fw_out;
  300. }
  301. crc_file = get_unaligned_le32(fw->data + regs_num * FW_RECORD_SIZE);
  302. crc = crc32_le(~0, fw->data, regs_num * FW_RECORD_SIZE);
  303. if (crc != crc_file) {
  304. v4l2_err(sd, "FW: invalid crc (%#x:%#x)\n", crc, crc_file);
  305. err = -EINVAL;
  306. goto fw_out;
  307. }
  308. ptr = fw->data + FW_RECORD_SIZE;
  309. for (i = 1; i < regs_num; i++) {
  310. addr = get_unaligned_le32(ptr);
  311. ptr += sizeof(u32);
  312. val = get_unaligned_le16(ptr);
  313. ptr += sizeof(u16);
  314. if (addr - addr_inc != 2)
  315. err = s5k4ecgx_write(client, addr, val);
  316. else
  317. err = s5k4ecgx_i2c_write(client, REG_CMDBUF0_ADDR, val);
  318. if (err)
  319. break;
  320. addr_inc = addr;
  321. }
  322. fw_out:
  323. release_firmware(fw);
  324. return err;
  325. }
  326. /* Set preview and capture input window */
  327. static int s5k4ecgx_set_input_window(struct i2c_client *c,
  328. const struct v4l2_rect *r)
  329. {
  330. int ret;
  331. ret = s5k4ecgx_write(c, REG_G_PREV_IN_WIDTH, r->width);
  332. if (!ret)
  333. ret = s5k4ecgx_write(c, REG_G_PREV_IN_HEIGHT, r->height);
  334. if (!ret)
  335. ret = s5k4ecgx_write(c, REG_G_PREV_IN_XOFFS, r->left);
  336. if (!ret)
  337. ret = s5k4ecgx_write(c, REG_G_PREV_IN_YOFFS, r->top);
  338. if (!ret)
  339. ret = s5k4ecgx_write(c, REG_G_CAP_IN_WIDTH, r->width);
  340. if (!ret)
  341. ret = s5k4ecgx_write(c, REG_G_CAP_IN_HEIGHT, r->height);
  342. if (!ret)
  343. ret = s5k4ecgx_write(c, REG_G_CAP_IN_XOFFS, r->left);
  344. if (!ret)
  345. ret = s5k4ecgx_write(c, REG_G_CAP_IN_YOFFS, r->top);
  346. return ret;
  347. }
  348. /* Set preview and capture zoom input window */
  349. static int s5k4ecgx_set_zoom_window(struct i2c_client *c,
  350. const struct v4l2_rect *r)
  351. {
  352. int ret;
  353. ret = s5k4ecgx_write(c, REG_G_PREVZOOM_IN_WIDTH, r->width);
  354. if (!ret)
  355. ret = s5k4ecgx_write(c, REG_G_PREVZOOM_IN_HEIGHT, r->height);
  356. if (!ret)
  357. ret = s5k4ecgx_write(c, REG_G_PREVZOOM_IN_XOFFS, r->left);
  358. if (!ret)
  359. ret = s5k4ecgx_write(c, REG_G_PREVZOOM_IN_YOFFS, r->top);
  360. if (!ret)
  361. ret = s5k4ecgx_write(c, REG_G_CAPZOOM_IN_WIDTH, r->width);
  362. if (!ret)
  363. ret = s5k4ecgx_write(c, REG_G_CAPZOOM_IN_HEIGHT, r->height);
  364. if (!ret)
  365. ret = s5k4ecgx_write(c, REG_G_CAPZOOM_IN_XOFFS, r->left);
  366. if (!ret)
  367. ret = s5k4ecgx_write(c, REG_G_CAPZOOM_IN_YOFFS, r->top);
  368. return ret;
  369. }
  370. static int s5k4ecgx_set_output_framefmt(struct s5k4ecgx *priv)
  371. {
  372. struct i2c_client *client = v4l2_get_subdevdata(&priv->sd);
  373. int ret;
  374. ret = s5k4ecgx_write(client, REG_P_OUT_WIDTH(0),
  375. priv->curr_frmsize->size.width);
  376. if (!ret)
  377. ret = s5k4ecgx_write(client, REG_P_OUT_HEIGHT(0),
  378. priv->curr_frmsize->size.height);
  379. if (!ret)
  380. ret = s5k4ecgx_write(client, REG_P_FMT(0),
  381. priv->curr_pixfmt->reg_p_format);
  382. return ret;
  383. }
  384. static int s5k4ecgx_init_sensor(struct v4l2_subdev *sd)
  385. {
  386. int ret;
  387. ret = s5k4ecgx_set_ahb_address(sd);
  388. /* The delay is from manufacturer's settings */
  389. msleep(100);
  390. if (!ret)
  391. ret = s5k4ecgx_load_firmware(sd);
  392. if (ret)
  393. v4l2_err(sd, "Failed to write initial settings\n");
  394. return ret;
  395. }
  396. static int s5k4ecgx_gpio_set_value(struct s5k4ecgx *priv, int id, u32 val)
  397. {
  398. if (!gpio_is_valid(priv->gpio[id].gpio))
  399. return 0;
  400. gpio_set_value(priv->gpio[id].gpio, val);
  401. return 1;
  402. }
  403. static int __s5k4ecgx_power_on(struct s5k4ecgx *priv)
  404. {
  405. int ret;
  406. ret = regulator_bulk_enable(S5K4ECGX_NUM_SUPPLIES, priv->supplies);
  407. if (ret)
  408. return ret;
  409. usleep_range(30, 50);
  410. /* The polarity of STBY is controlled by TSP */
  411. if (s5k4ecgx_gpio_set_value(priv, STBY, priv->gpio[STBY].level))
  412. usleep_range(30, 50);
  413. if (s5k4ecgx_gpio_set_value(priv, RST, priv->gpio[RST].level))
  414. usleep_range(30, 50);
  415. return 0;
  416. }
  417. static int __s5k4ecgx_power_off(struct s5k4ecgx *priv)
  418. {
  419. if (s5k4ecgx_gpio_set_value(priv, RST, !priv->gpio[RST].level))
  420. usleep_range(30, 50);
  421. if (s5k4ecgx_gpio_set_value(priv, STBY, !priv->gpio[STBY].level))
  422. usleep_range(30, 50);
  423. priv->streaming = 0;
  424. return regulator_bulk_disable(S5K4ECGX_NUM_SUPPLIES, priv->supplies);
  425. }
  426. /* Find nearest matching image pixel size. */
  427. static int s5k4ecgx_try_frame_size(struct v4l2_mbus_framefmt *mf,
  428. const struct s5k4ecgx_frmsize **size)
  429. {
  430. unsigned int min_err = ~0;
  431. int i = ARRAY_SIZE(s5k4ecgx_prev_sizes);
  432. const struct s5k4ecgx_frmsize *fsize = &s5k4ecgx_prev_sizes[0],
  433. *match = NULL;
  434. while (i--) {
  435. int err = abs(fsize->size.width - mf->width)
  436. + abs(fsize->size.height - mf->height);
  437. if (err < min_err) {
  438. min_err = err;
  439. match = fsize;
  440. }
  441. fsize++;
  442. }
  443. if (match) {
  444. mf->width = match->size.width;
  445. mf->height = match->size.height;
  446. if (size)
  447. *size = match;
  448. return 0;
  449. }
  450. return -EINVAL;
  451. }
  452. static int s5k4ecgx_enum_mbus_code(struct v4l2_subdev *sd,
  453. struct v4l2_subdev_pad_config *cfg,
  454. struct v4l2_subdev_mbus_code_enum *code)
  455. {
  456. if (code->index >= ARRAY_SIZE(s5k4ecgx_formats))
  457. return -EINVAL;
  458. code->code = s5k4ecgx_formats[code->index].code;
  459. return 0;
  460. }
  461. static int s5k4ecgx_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
  462. struct v4l2_subdev_format *fmt)
  463. {
  464. struct s5k4ecgx *priv = to_s5k4ecgx(sd);
  465. struct v4l2_mbus_framefmt *mf;
  466. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  467. if (cfg) {
  468. mf = v4l2_subdev_get_try_format(sd, cfg, 0);
  469. fmt->format = *mf;
  470. }
  471. return 0;
  472. }
  473. mf = &fmt->format;
  474. mutex_lock(&priv->lock);
  475. mf->width = priv->curr_frmsize->size.width;
  476. mf->height = priv->curr_frmsize->size.height;
  477. mf->code = priv->curr_pixfmt->code;
  478. mf->colorspace = priv->curr_pixfmt->colorspace;
  479. mf->field = V4L2_FIELD_NONE;
  480. mutex_unlock(&priv->lock);
  481. return 0;
  482. }
  483. static const struct s5k4ecgx_pixfmt *s5k4ecgx_try_fmt(struct v4l2_subdev *sd,
  484. struct v4l2_mbus_framefmt *mf)
  485. {
  486. int i = ARRAY_SIZE(s5k4ecgx_formats);
  487. while (--i)
  488. if (mf->code == s5k4ecgx_formats[i].code)
  489. break;
  490. mf->code = s5k4ecgx_formats[i].code;
  491. return &s5k4ecgx_formats[i];
  492. }
  493. static int s5k4ecgx_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
  494. struct v4l2_subdev_format *fmt)
  495. {
  496. struct s5k4ecgx *priv = to_s5k4ecgx(sd);
  497. const struct s5k4ecgx_frmsize *fsize = NULL;
  498. const struct s5k4ecgx_pixfmt *pf;
  499. struct v4l2_mbus_framefmt *mf;
  500. int ret = 0;
  501. pf = s5k4ecgx_try_fmt(sd, &fmt->format);
  502. s5k4ecgx_try_frame_size(&fmt->format, &fsize);
  503. fmt->format.colorspace = V4L2_COLORSPACE_JPEG;
  504. fmt->format.field = V4L2_FIELD_NONE;
  505. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  506. if (cfg) {
  507. mf = v4l2_subdev_get_try_format(sd, cfg, 0);
  508. *mf = fmt->format;
  509. }
  510. return 0;
  511. }
  512. mutex_lock(&priv->lock);
  513. if (!priv->streaming) {
  514. priv->curr_frmsize = fsize;
  515. priv->curr_pixfmt = pf;
  516. priv->set_params = 1;
  517. } else {
  518. ret = -EBUSY;
  519. }
  520. mutex_unlock(&priv->lock);
  521. return ret;
  522. }
  523. static const struct v4l2_subdev_pad_ops s5k4ecgx_pad_ops = {
  524. .enum_mbus_code = s5k4ecgx_enum_mbus_code,
  525. .get_fmt = s5k4ecgx_get_fmt,
  526. .set_fmt = s5k4ecgx_set_fmt,
  527. };
  528. /*
  529. * V4L2 subdev controls
  530. */
  531. static int s5k4ecgx_s_ctrl(struct v4l2_ctrl *ctrl)
  532. {
  533. struct v4l2_subdev *sd = &container_of(ctrl->handler, struct s5k4ecgx,
  534. handler)->sd;
  535. struct i2c_client *client = v4l2_get_subdevdata(sd);
  536. struct s5k4ecgx *priv = to_s5k4ecgx(sd);
  537. unsigned int i;
  538. int err = 0;
  539. v4l2_dbg(1, debug, sd, "ctrl: 0x%x, value: %d\n", ctrl->id, ctrl->val);
  540. mutex_lock(&priv->lock);
  541. switch (ctrl->id) {
  542. case V4L2_CID_CONTRAST:
  543. err = s5k4ecgx_write(client, REG_USER_CONTRAST, ctrl->val);
  544. break;
  545. case V4L2_CID_SATURATION:
  546. err = s5k4ecgx_write(client, REG_USER_SATURATION, ctrl->val);
  547. break;
  548. case V4L2_CID_SHARPNESS:
  549. /* TODO: Revisit, is this setting for all presets ? */
  550. for (i = 0; i < 4 && !err; i++)
  551. err = s5k4ecgx_write(client, REG_USER_SHARPNESS(i),
  552. ctrl->val * SHARPNESS_DIV);
  553. break;
  554. case V4L2_CID_BRIGHTNESS:
  555. err = s5k4ecgx_write(client, REG_USER_BRIGHTNESS, ctrl->val);
  556. break;
  557. }
  558. mutex_unlock(&priv->lock);
  559. if (err < 0)
  560. v4l2_err(sd, "Failed to write s_ctrl err %d\n", err);
  561. return err;
  562. }
  563. static const struct v4l2_ctrl_ops s5k4ecgx_ctrl_ops = {
  564. .s_ctrl = s5k4ecgx_s_ctrl,
  565. };
  566. /*
  567. * Reading s5k4ecgx version information
  568. */
  569. static int s5k4ecgx_registered(struct v4l2_subdev *sd)
  570. {
  571. int ret;
  572. struct s5k4ecgx *priv = to_s5k4ecgx(sd);
  573. mutex_lock(&priv->lock);
  574. ret = __s5k4ecgx_power_on(priv);
  575. if (!ret) {
  576. ret = s5k4ecgx_read_fw_ver(sd);
  577. __s5k4ecgx_power_off(priv);
  578. }
  579. mutex_unlock(&priv->lock);
  580. return ret;
  581. }
  582. /*
  583. * V4L2 subdev internal operations
  584. */
  585. static int s5k4ecgx_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  586. {
  587. struct v4l2_mbus_framefmt *mf = v4l2_subdev_get_try_format(sd, fh->pad, 0);
  588. mf->width = s5k4ecgx_prev_sizes[0].size.width;
  589. mf->height = s5k4ecgx_prev_sizes[0].size.height;
  590. mf->code = s5k4ecgx_formats[0].code;
  591. mf->colorspace = V4L2_COLORSPACE_JPEG;
  592. mf->field = V4L2_FIELD_NONE;
  593. return 0;
  594. }
  595. static const struct v4l2_subdev_internal_ops s5k4ecgx_subdev_internal_ops = {
  596. .registered = s5k4ecgx_registered,
  597. .open = s5k4ecgx_open,
  598. };
  599. static int s5k4ecgx_s_power(struct v4l2_subdev *sd, int on)
  600. {
  601. struct s5k4ecgx *priv = to_s5k4ecgx(sd);
  602. int ret;
  603. v4l2_dbg(1, debug, sd, "Switching %s\n", on ? "on" : "off");
  604. if (on) {
  605. ret = __s5k4ecgx_power_on(priv);
  606. if (ret < 0)
  607. return ret;
  608. /* Time to stabilize sensor */
  609. msleep(100);
  610. ret = s5k4ecgx_init_sensor(sd);
  611. if (ret < 0)
  612. __s5k4ecgx_power_off(priv);
  613. else
  614. priv->set_params = 1;
  615. } else {
  616. ret = __s5k4ecgx_power_off(priv);
  617. }
  618. return ret;
  619. }
  620. static int s5k4ecgx_log_status(struct v4l2_subdev *sd)
  621. {
  622. v4l2_ctrl_handler_log_status(sd->ctrl_handler, sd->name);
  623. return 0;
  624. }
  625. static const struct v4l2_subdev_core_ops s5k4ecgx_core_ops = {
  626. .s_power = s5k4ecgx_s_power,
  627. .log_status = s5k4ecgx_log_status,
  628. };
  629. static int __s5k4ecgx_s_params(struct s5k4ecgx *priv)
  630. {
  631. struct i2c_client *client = v4l2_get_subdevdata(&priv->sd);
  632. const struct v4l2_rect *crop_rect = &priv->curr_frmsize->input_window;
  633. int ret;
  634. ret = s5k4ecgx_set_input_window(client, crop_rect);
  635. if (!ret)
  636. ret = s5k4ecgx_set_zoom_window(client, crop_rect);
  637. if (!ret)
  638. ret = s5k4ecgx_write(client, REG_G_INPUTS_CHANGE_REQ, 1);
  639. if (!ret)
  640. ret = s5k4ecgx_write(client, 0x70000a1e, 0x28);
  641. if (!ret)
  642. ret = s5k4ecgx_write(client, 0x70000ad4, 0x3c);
  643. if (!ret)
  644. ret = s5k4ecgx_set_output_framefmt(priv);
  645. if (!ret)
  646. ret = s5k4ecgx_write(client, REG_P_PVI_MASK(0), 0x52);
  647. if (!ret)
  648. ret = s5k4ecgx_write(client, REG_P_FR_TIME_TYPE(0),
  649. FR_TIME_DYNAMIC);
  650. if (!ret)
  651. ret = s5k4ecgx_write(client, REG_P_FR_TIME_Q_TYPE(0),
  652. FR_TIME_Q_BEST_FRRATE);
  653. if (!ret)
  654. ret = s5k4ecgx_write(client, REG_P_MIN_FR_TIME(0),
  655. US_TO_FR_TIME(33300));
  656. if (!ret)
  657. ret = s5k4ecgx_write(client, REG_P_MAX_FR_TIME(0),
  658. US_TO_FR_TIME(66600));
  659. if (!ret)
  660. ret = s5k4ecgx_write(client, REG_P_PREV_MIRROR(0), 0);
  661. if (!ret)
  662. ret = s5k4ecgx_write(client, REG_P_CAP_MIRROR(0), 0);
  663. if (!ret)
  664. ret = s5k4ecgx_write(client, REG_G_ACTIVE_PREV_CFG, 0);
  665. if (!ret)
  666. ret = s5k4ecgx_write(client, REG_G_PREV_OPEN_AFTER_CH, 1);
  667. if (!ret)
  668. ret = s5k4ecgx_write(client, REG_G_NEW_CFG_SYNC, 1);
  669. if (!ret)
  670. ret = s5k4ecgx_write(client, REG_G_PREV_CFG_CHG, 1);
  671. return ret;
  672. }
  673. static int __s5k4ecgx_s_stream(struct s5k4ecgx *priv, int on)
  674. {
  675. struct i2c_client *client = v4l2_get_subdevdata(&priv->sd);
  676. int ret;
  677. if (on && priv->set_params) {
  678. ret = __s5k4ecgx_s_params(priv);
  679. if (ret < 0)
  680. return ret;
  681. priv->set_params = 0;
  682. }
  683. /*
  684. * This enables/disables preview stream only. Capture requests
  685. * are not supported yet.
  686. */
  687. ret = s5k4ecgx_write(client, REG_G_ENABLE_PREV, on);
  688. if (ret < 0)
  689. return ret;
  690. return s5k4ecgx_write(client, REG_G_ENABLE_PREV_CHG, 1);
  691. }
  692. static int s5k4ecgx_s_stream(struct v4l2_subdev *sd, int on)
  693. {
  694. struct s5k4ecgx *priv = to_s5k4ecgx(sd);
  695. int ret = 0;
  696. v4l2_dbg(1, debug, sd, "Turn streaming %s\n", on ? "on" : "off");
  697. mutex_lock(&priv->lock);
  698. if (priv->streaming == !on) {
  699. ret = __s5k4ecgx_s_stream(priv, on);
  700. if (!ret)
  701. priv->streaming = on & 1;
  702. }
  703. mutex_unlock(&priv->lock);
  704. return ret;
  705. }
  706. static const struct v4l2_subdev_video_ops s5k4ecgx_video_ops = {
  707. .s_stream = s5k4ecgx_s_stream,
  708. };
  709. static const struct v4l2_subdev_ops s5k4ecgx_ops = {
  710. .core = &s5k4ecgx_core_ops,
  711. .pad = &s5k4ecgx_pad_ops,
  712. .video = &s5k4ecgx_video_ops,
  713. };
  714. /*
  715. * GPIO setup
  716. */
  717. static int s5k4ecgx_config_gpio(int nr, int val, const char *name)
  718. {
  719. unsigned long flags = val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  720. int ret;
  721. if (!gpio_is_valid(nr))
  722. return 0;
  723. ret = gpio_request_one(nr, flags, name);
  724. if (!ret)
  725. gpio_export(nr, 0);
  726. return ret;
  727. }
  728. static void s5k4ecgx_free_gpios(struct s5k4ecgx *priv)
  729. {
  730. int i;
  731. for (i = 0; i < ARRAY_SIZE(priv->gpio); i++) {
  732. if (!gpio_is_valid(priv->gpio[i].gpio))
  733. continue;
  734. gpio_free(priv->gpio[i].gpio);
  735. priv->gpio[i].gpio = -EINVAL;
  736. }
  737. }
  738. static int s5k4ecgx_config_gpios(struct s5k4ecgx *priv,
  739. const struct s5k4ecgx_platform_data *pdata)
  740. {
  741. const struct s5k4ecgx_gpio *gpio = &pdata->gpio_stby;
  742. int ret;
  743. priv->gpio[STBY].gpio = -EINVAL;
  744. priv->gpio[RST].gpio = -EINVAL;
  745. ret = s5k4ecgx_config_gpio(gpio->gpio, gpio->level, "S5K4ECGX_STBY");
  746. if (ret) {
  747. s5k4ecgx_free_gpios(priv);
  748. return ret;
  749. }
  750. priv->gpio[STBY] = *gpio;
  751. if (gpio_is_valid(gpio->gpio))
  752. gpio_set_value(gpio->gpio, 0);
  753. gpio = &pdata->gpio_reset;
  754. ret = s5k4ecgx_config_gpio(gpio->gpio, gpio->level, "S5K4ECGX_RST");
  755. if (ret) {
  756. s5k4ecgx_free_gpios(priv);
  757. return ret;
  758. }
  759. priv->gpio[RST] = *gpio;
  760. if (gpio_is_valid(gpio->gpio))
  761. gpio_set_value(gpio->gpio, 0);
  762. return 0;
  763. }
  764. static int s5k4ecgx_init_v4l2_ctrls(struct s5k4ecgx *priv)
  765. {
  766. const struct v4l2_ctrl_ops *ops = &s5k4ecgx_ctrl_ops;
  767. struct v4l2_ctrl_handler *hdl = &priv->handler;
  768. int ret;
  769. ret = v4l2_ctrl_handler_init(hdl, 4);
  770. if (ret)
  771. return ret;
  772. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS, -208, 127, 1, 0);
  773. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, -127, 127, 1, 0);
  774. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, -127, 127, 1, 0);
  775. /* Sharpness default is 24612, and then (24612/SHARPNESS_DIV) = 2 */
  776. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SHARPNESS, -32704/SHARPNESS_DIV,
  777. 24612/SHARPNESS_DIV, 1, 2);
  778. if (hdl->error) {
  779. ret = hdl->error;
  780. v4l2_ctrl_handler_free(hdl);
  781. return ret;
  782. }
  783. priv->sd.ctrl_handler = hdl;
  784. return 0;
  785. };
  786. static int s5k4ecgx_probe(struct i2c_client *client,
  787. const struct i2c_device_id *id)
  788. {
  789. struct s5k4ecgx_platform_data *pdata = client->dev.platform_data;
  790. struct v4l2_subdev *sd;
  791. struct s5k4ecgx *priv;
  792. int ret, i;
  793. if (pdata == NULL) {
  794. dev_err(&client->dev, "platform data is missing!\n");
  795. return -EINVAL;
  796. }
  797. priv = devm_kzalloc(&client->dev, sizeof(struct s5k4ecgx), GFP_KERNEL);
  798. if (!priv)
  799. return -ENOMEM;
  800. mutex_init(&priv->lock);
  801. priv->streaming = 0;
  802. sd = &priv->sd;
  803. /* Registering subdev */
  804. v4l2_i2c_subdev_init(sd, client, &s5k4ecgx_ops);
  805. strlcpy(sd->name, S5K4ECGX_DRIVER_NAME, sizeof(sd->name));
  806. sd->internal_ops = &s5k4ecgx_subdev_internal_ops;
  807. /* Support v4l2 sub-device user space API */
  808. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  809. priv->pad.flags = MEDIA_PAD_FL_SOURCE;
  810. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
  811. ret = media_entity_init(&sd->entity, 1, &priv->pad, 0);
  812. if (ret)
  813. return ret;
  814. ret = s5k4ecgx_config_gpios(priv, pdata);
  815. if (ret) {
  816. dev_err(&client->dev, "Failed to set gpios\n");
  817. goto out_err1;
  818. }
  819. for (i = 0; i < S5K4ECGX_NUM_SUPPLIES; i++)
  820. priv->supplies[i].supply = s5k4ecgx_supply_names[i];
  821. ret = devm_regulator_bulk_get(&client->dev, S5K4ECGX_NUM_SUPPLIES,
  822. priv->supplies);
  823. if (ret) {
  824. dev_err(&client->dev, "Failed to get regulators\n");
  825. goto out_err2;
  826. }
  827. ret = s5k4ecgx_init_v4l2_ctrls(priv);
  828. if (ret)
  829. goto out_err2;
  830. priv->curr_pixfmt = &s5k4ecgx_formats[0];
  831. priv->curr_frmsize = &s5k4ecgx_prev_sizes[0];
  832. return 0;
  833. out_err2:
  834. s5k4ecgx_free_gpios(priv);
  835. out_err1:
  836. media_entity_cleanup(&priv->sd.entity);
  837. return ret;
  838. }
  839. static int s5k4ecgx_remove(struct i2c_client *client)
  840. {
  841. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  842. struct s5k4ecgx *priv = to_s5k4ecgx(sd);
  843. mutex_destroy(&priv->lock);
  844. s5k4ecgx_free_gpios(priv);
  845. v4l2_device_unregister_subdev(sd);
  846. v4l2_ctrl_handler_free(&priv->handler);
  847. media_entity_cleanup(&sd->entity);
  848. return 0;
  849. }
  850. static const struct i2c_device_id s5k4ecgx_id[] = {
  851. { S5K4ECGX_DRIVER_NAME, 0 },
  852. {}
  853. };
  854. MODULE_DEVICE_TABLE(i2c, s5k4ecgx_id);
  855. static struct i2c_driver v4l2_i2c_driver = {
  856. .driver = {
  857. .owner = THIS_MODULE,
  858. .name = S5K4ECGX_DRIVER_NAME,
  859. },
  860. .probe = s5k4ecgx_probe,
  861. .remove = s5k4ecgx_remove,
  862. .id_table = s5k4ecgx_id,
  863. };
  864. module_i2c_driver(v4l2_i2c_driver);
  865. MODULE_DESCRIPTION("Samsung S5K4ECGX 5MP SOC camera");
  866. MODULE_AUTHOR("Sangwook Lee <sangwook.lee@linaro.org>");
  867. MODULE_AUTHOR("Seok-Young Jang <quartz.jang@samsung.com>");
  868. MODULE_LICENSE("GPL");
  869. MODULE_FIRMWARE(S5K4ECGX_FIRMWARE);