s5k4ecgx.c 26 KB

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