s5k4ecgx.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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(*((u16 *)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 = le32_to_cpu(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 = le32_to_cpu(get_unaligned_le32(fw->data +
  302. regs_num * FW_RECORD_SIZE));
  303. crc = crc32_le(~0, fw->data, regs_num * FW_RECORD_SIZE);
  304. if (crc != crc_file) {
  305. v4l2_err(sd, "FW: invalid crc (%#x:%#x)\n", crc, crc_file);
  306. err = -EINVAL;
  307. goto fw_out;
  308. }
  309. ptr = fw->data + FW_RECORD_SIZE;
  310. for (i = 1; i < regs_num; i++) {
  311. addr = le32_to_cpu(get_unaligned_le32(ptr));
  312. ptr += sizeof(u32);
  313. val = le16_to_cpu(get_unaligned_le16(ptr));
  314. ptr += sizeof(u16);
  315. if (addr - addr_inc != 2)
  316. err = s5k4ecgx_write(client, addr, val);
  317. else
  318. err = s5k4ecgx_i2c_write(client, REG_CMDBUF0_ADDR, val);
  319. if (err)
  320. break;
  321. addr_inc = addr;
  322. }
  323. fw_out:
  324. release_firmware(fw);
  325. return err;
  326. }
  327. /* Set preview and capture input window */
  328. static int s5k4ecgx_set_input_window(struct i2c_client *c,
  329. const struct v4l2_rect *r)
  330. {
  331. int ret;
  332. ret = s5k4ecgx_write(c, REG_G_PREV_IN_WIDTH, r->width);
  333. if (!ret)
  334. ret = s5k4ecgx_write(c, REG_G_PREV_IN_HEIGHT, r->height);
  335. if (!ret)
  336. ret = s5k4ecgx_write(c, REG_G_PREV_IN_XOFFS, r->left);
  337. if (!ret)
  338. ret = s5k4ecgx_write(c, REG_G_PREV_IN_YOFFS, r->top);
  339. if (!ret)
  340. ret = s5k4ecgx_write(c, REG_G_CAP_IN_WIDTH, r->width);
  341. if (!ret)
  342. ret = s5k4ecgx_write(c, REG_G_CAP_IN_HEIGHT, r->height);
  343. if (!ret)
  344. ret = s5k4ecgx_write(c, REG_G_CAP_IN_XOFFS, r->left);
  345. if (!ret)
  346. ret = s5k4ecgx_write(c, REG_G_CAP_IN_YOFFS, r->top);
  347. return ret;
  348. }
  349. /* Set preview and capture zoom input window */
  350. static int s5k4ecgx_set_zoom_window(struct i2c_client *c,
  351. const struct v4l2_rect *r)
  352. {
  353. int ret;
  354. ret = s5k4ecgx_write(c, REG_G_PREVZOOM_IN_WIDTH, r->width);
  355. if (!ret)
  356. ret = s5k4ecgx_write(c, REG_G_PREVZOOM_IN_HEIGHT, r->height);
  357. if (!ret)
  358. ret = s5k4ecgx_write(c, REG_G_PREVZOOM_IN_XOFFS, r->left);
  359. if (!ret)
  360. ret = s5k4ecgx_write(c, REG_G_PREVZOOM_IN_YOFFS, r->top);
  361. if (!ret)
  362. ret = s5k4ecgx_write(c, REG_G_CAPZOOM_IN_WIDTH, r->width);
  363. if (!ret)
  364. ret = s5k4ecgx_write(c, REG_G_CAPZOOM_IN_HEIGHT, r->height);
  365. if (!ret)
  366. ret = s5k4ecgx_write(c, REG_G_CAPZOOM_IN_XOFFS, r->left);
  367. if (!ret)
  368. ret = s5k4ecgx_write(c, REG_G_CAPZOOM_IN_YOFFS, r->top);
  369. return ret;
  370. }
  371. static int s5k4ecgx_set_output_framefmt(struct s5k4ecgx *priv)
  372. {
  373. struct i2c_client *client = v4l2_get_subdevdata(&priv->sd);
  374. int ret;
  375. ret = s5k4ecgx_write(client, REG_P_OUT_WIDTH(0),
  376. priv->curr_frmsize->size.width);
  377. if (!ret)
  378. ret = s5k4ecgx_write(client, REG_P_OUT_HEIGHT(0),
  379. priv->curr_frmsize->size.height);
  380. if (!ret)
  381. ret = s5k4ecgx_write(client, REG_P_FMT(0),
  382. priv->curr_pixfmt->reg_p_format);
  383. return ret;
  384. }
  385. static int s5k4ecgx_init_sensor(struct v4l2_subdev *sd)
  386. {
  387. int ret;
  388. ret = s5k4ecgx_set_ahb_address(sd);
  389. /* The delay is from manufacturer's settings */
  390. msleep(100);
  391. if (!ret)
  392. ret = s5k4ecgx_load_firmware(sd);
  393. if (ret)
  394. v4l2_err(sd, "Failed to write initial settings\n");
  395. return ret;
  396. }
  397. static int s5k4ecgx_gpio_set_value(struct s5k4ecgx *priv, int id, u32 val)
  398. {
  399. if (!gpio_is_valid(priv->gpio[id].gpio))
  400. return 0;
  401. gpio_set_value(priv->gpio[id].gpio, val);
  402. return 1;
  403. }
  404. static int __s5k4ecgx_power_on(struct s5k4ecgx *priv)
  405. {
  406. int ret;
  407. ret = regulator_bulk_enable(S5K4ECGX_NUM_SUPPLIES, priv->supplies);
  408. if (ret)
  409. return ret;
  410. usleep_range(30, 50);
  411. /* The polarity of STBY is controlled by TSP */
  412. if (s5k4ecgx_gpio_set_value(priv, STBY, priv->gpio[STBY].level))
  413. usleep_range(30, 50);
  414. if (s5k4ecgx_gpio_set_value(priv, RST, priv->gpio[RST].level))
  415. usleep_range(30, 50);
  416. return 0;
  417. }
  418. static int __s5k4ecgx_power_off(struct s5k4ecgx *priv)
  419. {
  420. if (s5k4ecgx_gpio_set_value(priv, RST, !priv->gpio[RST].level))
  421. usleep_range(30, 50);
  422. if (s5k4ecgx_gpio_set_value(priv, STBY, !priv->gpio[STBY].level))
  423. usleep_range(30, 50);
  424. priv->streaming = 0;
  425. return regulator_bulk_disable(S5K4ECGX_NUM_SUPPLIES, priv->supplies);
  426. }
  427. /* Find nearest matching image pixel size. */
  428. static int s5k4ecgx_try_frame_size(struct v4l2_mbus_framefmt *mf,
  429. const struct s5k4ecgx_frmsize **size)
  430. {
  431. unsigned int min_err = ~0;
  432. int i = ARRAY_SIZE(s5k4ecgx_prev_sizes);
  433. const struct s5k4ecgx_frmsize *fsize = &s5k4ecgx_prev_sizes[0],
  434. *match = NULL;
  435. while (i--) {
  436. int err = abs(fsize->size.width - mf->width)
  437. + abs(fsize->size.height - mf->height);
  438. if (err < min_err) {
  439. min_err = err;
  440. match = fsize;
  441. }
  442. fsize++;
  443. }
  444. if (match) {
  445. mf->width = match->size.width;
  446. mf->height = match->size.height;
  447. if (size)
  448. *size = match;
  449. return 0;
  450. }
  451. return -EINVAL;
  452. }
  453. static int s5k4ecgx_enum_mbus_code(struct v4l2_subdev *sd,
  454. struct v4l2_subdev_fh *fh,
  455. struct v4l2_subdev_mbus_code_enum *code)
  456. {
  457. if (code->index >= ARRAY_SIZE(s5k4ecgx_formats))
  458. return -EINVAL;
  459. code->code = s5k4ecgx_formats[code->index].code;
  460. return 0;
  461. }
  462. static int s5k4ecgx_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  463. struct v4l2_subdev_format *fmt)
  464. {
  465. struct s5k4ecgx *priv = to_s5k4ecgx(sd);
  466. struct v4l2_mbus_framefmt *mf;
  467. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  468. if (fh) {
  469. mf = v4l2_subdev_get_try_format(fh, 0);
  470. fmt->format = *mf;
  471. }
  472. return 0;
  473. }
  474. mf = &fmt->format;
  475. mutex_lock(&priv->lock);
  476. mf->width = priv->curr_frmsize->size.width;
  477. mf->height = priv->curr_frmsize->size.height;
  478. mf->code = priv->curr_pixfmt->code;
  479. mf->colorspace = priv->curr_pixfmt->colorspace;
  480. mf->field = V4L2_FIELD_NONE;
  481. mutex_unlock(&priv->lock);
  482. return 0;
  483. }
  484. static const struct s5k4ecgx_pixfmt *s5k4ecgx_try_fmt(struct v4l2_subdev *sd,
  485. struct v4l2_mbus_framefmt *mf)
  486. {
  487. int i = ARRAY_SIZE(s5k4ecgx_formats);
  488. while (--i)
  489. if (mf->code == s5k4ecgx_formats[i].code)
  490. break;
  491. mf->code = s5k4ecgx_formats[i].code;
  492. return &s5k4ecgx_formats[i];
  493. }
  494. static int s5k4ecgx_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  495. struct v4l2_subdev_format *fmt)
  496. {
  497. struct s5k4ecgx *priv = to_s5k4ecgx(sd);
  498. const struct s5k4ecgx_frmsize *fsize = NULL;
  499. const struct s5k4ecgx_pixfmt *pf;
  500. struct v4l2_mbus_framefmt *mf;
  501. int ret = 0;
  502. pf = s5k4ecgx_try_fmt(sd, &fmt->format);
  503. s5k4ecgx_try_frame_size(&fmt->format, &fsize);
  504. fmt->format.colorspace = V4L2_COLORSPACE_JPEG;
  505. fmt->format.field = V4L2_FIELD_NONE;
  506. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  507. if (fh) {
  508. mf = v4l2_subdev_get_try_format(fh, 0);
  509. *mf = fmt->format;
  510. }
  511. return 0;
  512. }
  513. mutex_lock(&priv->lock);
  514. if (!priv->streaming) {
  515. priv->curr_frmsize = fsize;
  516. priv->curr_pixfmt = pf;
  517. priv->set_params = 1;
  518. } else {
  519. ret = -EBUSY;
  520. }
  521. mutex_unlock(&priv->lock);
  522. return ret;
  523. }
  524. static const struct v4l2_subdev_pad_ops s5k4ecgx_pad_ops = {
  525. .enum_mbus_code = s5k4ecgx_enum_mbus_code,
  526. .get_fmt = s5k4ecgx_get_fmt,
  527. .set_fmt = s5k4ecgx_set_fmt,
  528. };
  529. /*
  530. * V4L2 subdev controls
  531. */
  532. static int s5k4ecgx_s_ctrl(struct v4l2_ctrl *ctrl)
  533. {
  534. struct v4l2_subdev *sd = &container_of(ctrl->handler, struct s5k4ecgx,
  535. handler)->sd;
  536. struct i2c_client *client = v4l2_get_subdevdata(sd);
  537. struct s5k4ecgx *priv = to_s5k4ecgx(sd);
  538. unsigned int i;
  539. int err = 0;
  540. v4l2_dbg(1, debug, sd, "ctrl: 0x%x, value: %d\n", ctrl->id, ctrl->val);
  541. mutex_lock(&priv->lock);
  542. switch (ctrl->id) {
  543. case V4L2_CID_CONTRAST:
  544. err = s5k4ecgx_write(client, REG_USER_CONTRAST, ctrl->val);
  545. break;
  546. case V4L2_CID_SATURATION:
  547. err = s5k4ecgx_write(client, REG_USER_SATURATION, ctrl->val);
  548. break;
  549. case V4L2_CID_SHARPNESS:
  550. /* TODO: Revisit, is this setting for all presets ? */
  551. for (i = 0; i < 4 && !err; i++)
  552. err = s5k4ecgx_write(client, REG_USER_SHARPNESS(i),
  553. ctrl->val * SHARPNESS_DIV);
  554. break;
  555. case V4L2_CID_BRIGHTNESS:
  556. err = s5k4ecgx_write(client, REG_USER_BRIGHTNESS, ctrl->val);
  557. break;
  558. }
  559. mutex_unlock(&priv->lock);
  560. if (err < 0)
  561. v4l2_err(sd, "Failed to write s_ctrl err %d\n", err);
  562. return err;
  563. }
  564. static const struct v4l2_ctrl_ops s5k4ecgx_ctrl_ops = {
  565. .s_ctrl = s5k4ecgx_s_ctrl,
  566. };
  567. /*
  568. * Reading s5k4ecgx version information
  569. */
  570. static int s5k4ecgx_registered(struct v4l2_subdev *sd)
  571. {
  572. int ret;
  573. struct s5k4ecgx *priv = to_s5k4ecgx(sd);
  574. mutex_lock(&priv->lock);
  575. ret = __s5k4ecgx_power_on(priv);
  576. if (!ret) {
  577. ret = s5k4ecgx_read_fw_ver(sd);
  578. __s5k4ecgx_power_off(priv);
  579. }
  580. mutex_unlock(&priv->lock);
  581. return ret;
  582. }
  583. /*
  584. * V4L2 subdev internal operations
  585. */
  586. static int s5k4ecgx_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  587. {
  588. struct v4l2_mbus_framefmt *mf = v4l2_subdev_get_try_format(fh, 0);
  589. mf->width = s5k4ecgx_prev_sizes[0].size.width;
  590. mf->height = s5k4ecgx_prev_sizes[0].size.height;
  591. mf->code = s5k4ecgx_formats[0].code;
  592. mf->colorspace = V4L2_COLORSPACE_JPEG;
  593. mf->field = V4L2_FIELD_NONE;
  594. return 0;
  595. }
  596. static const struct v4l2_subdev_internal_ops s5k4ecgx_subdev_internal_ops = {
  597. .registered = s5k4ecgx_registered,
  598. .open = s5k4ecgx_open,
  599. };
  600. static int s5k4ecgx_s_power(struct v4l2_subdev *sd, int on)
  601. {
  602. struct s5k4ecgx *priv = to_s5k4ecgx(sd);
  603. int ret;
  604. v4l2_dbg(1, debug, sd, "Switching %s\n", on ? "on" : "off");
  605. if (on) {
  606. ret = __s5k4ecgx_power_on(priv);
  607. if (ret < 0)
  608. return ret;
  609. /* Time to stabilize sensor */
  610. msleep(100);
  611. ret = s5k4ecgx_init_sensor(sd);
  612. if (ret < 0)
  613. __s5k4ecgx_power_off(priv);
  614. else
  615. priv->set_params = 1;
  616. } else {
  617. ret = __s5k4ecgx_power_off(priv);
  618. }
  619. return ret;
  620. }
  621. static int s5k4ecgx_log_status(struct v4l2_subdev *sd)
  622. {
  623. v4l2_ctrl_handler_log_status(sd->ctrl_handler, sd->name);
  624. return 0;
  625. }
  626. static const struct v4l2_subdev_core_ops s5k4ecgx_core_ops = {
  627. .s_power = s5k4ecgx_s_power,
  628. .log_status = s5k4ecgx_log_status,
  629. };
  630. static int __s5k4ecgx_s_params(struct s5k4ecgx *priv)
  631. {
  632. struct i2c_client *client = v4l2_get_subdevdata(&priv->sd);
  633. const struct v4l2_rect *crop_rect = &priv->curr_frmsize->input_window;
  634. int ret;
  635. ret = s5k4ecgx_set_input_window(client, crop_rect);
  636. if (!ret)
  637. ret = s5k4ecgx_set_zoom_window(client, crop_rect);
  638. if (!ret)
  639. ret = s5k4ecgx_write(client, REG_G_INPUTS_CHANGE_REQ, 1);
  640. if (!ret)
  641. ret = s5k4ecgx_write(client, 0x70000a1e, 0x28);
  642. if (!ret)
  643. ret = s5k4ecgx_write(client, 0x70000ad4, 0x3c);
  644. if (!ret)
  645. ret = s5k4ecgx_set_output_framefmt(priv);
  646. if (!ret)
  647. ret = s5k4ecgx_write(client, REG_P_PVI_MASK(0), 0x52);
  648. if (!ret)
  649. ret = s5k4ecgx_write(client, REG_P_FR_TIME_TYPE(0),
  650. FR_TIME_DYNAMIC);
  651. if (!ret)
  652. ret = s5k4ecgx_write(client, REG_P_FR_TIME_Q_TYPE(0),
  653. FR_TIME_Q_BEST_FRRATE);
  654. if (!ret)
  655. ret = s5k4ecgx_write(client, REG_P_MIN_FR_TIME(0),
  656. US_TO_FR_TIME(33300));
  657. if (!ret)
  658. ret = s5k4ecgx_write(client, REG_P_MAX_FR_TIME(0),
  659. US_TO_FR_TIME(66600));
  660. if (!ret)
  661. ret = s5k4ecgx_write(client, REG_P_PREV_MIRROR(0), 0);
  662. if (!ret)
  663. ret = s5k4ecgx_write(client, REG_P_CAP_MIRROR(0), 0);
  664. if (!ret)
  665. ret = s5k4ecgx_write(client, REG_G_ACTIVE_PREV_CFG, 0);
  666. if (!ret)
  667. ret = s5k4ecgx_write(client, REG_G_PREV_OPEN_AFTER_CH, 1);
  668. if (!ret)
  669. ret = s5k4ecgx_write(client, REG_G_NEW_CFG_SYNC, 1);
  670. if (!ret)
  671. ret = s5k4ecgx_write(client, REG_G_PREV_CFG_CHG, 1);
  672. return ret;
  673. }
  674. static int __s5k4ecgx_s_stream(struct s5k4ecgx *priv, int on)
  675. {
  676. struct i2c_client *client = v4l2_get_subdevdata(&priv->sd);
  677. int ret;
  678. if (on && priv->set_params) {
  679. ret = __s5k4ecgx_s_params(priv);
  680. if (ret < 0)
  681. return ret;
  682. priv->set_params = 0;
  683. }
  684. /*
  685. * This enables/disables preview stream only. Capture requests
  686. * are not supported yet.
  687. */
  688. ret = s5k4ecgx_write(client, REG_G_ENABLE_PREV, on);
  689. if (ret < 0)
  690. return ret;
  691. return s5k4ecgx_write(client, REG_G_ENABLE_PREV_CHG, 1);
  692. }
  693. static int s5k4ecgx_s_stream(struct v4l2_subdev *sd, int on)
  694. {
  695. struct s5k4ecgx *priv = to_s5k4ecgx(sd);
  696. int ret = 0;
  697. v4l2_dbg(1, debug, sd, "Turn streaming %s\n", on ? "on" : "off");
  698. mutex_lock(&priv->lock);
  699. if (priv->streaming == !on) {
  700. ret = __s5k4ecgx_s_stream(priv, on);
  701. if (!ret)
  702. priv->streaming = on & 1;
  703. }
  704. mutex_unlock(&priv->lock);
  705. return ret;
  706. }
  707. static const struct v4l2_subdev_video_ops s5k4ecgx_video_ops = {
  708. .s_stream = s5k4ecgx_s_stream,
  709. };
  710. static const struct v4l2_subdev_ops s5k4ecgx_ops = {
  711. .core = &s5k4ecgx_core_ops,
  712. .pad = &s5k4ecgx_pad_ops,
  713. .video = &s5k4ecgx_video_ops,
  714. };
  715. /*
  716. * GPIO setup
  717. */
  718. static int s5k4ecgx_config_gpio(int nr, int val, const char *name)
  719. {
  720. unsigned long flags = val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  721. int ret;
  722. if (!gpio_is_valid(nr))
  723. return 0;
  724. ret = gpio_request_one(nr, flags, name);
  725. if (!ret)
  726. gpio_export(nr, 0);
  727. return ret;
  728. }
  729. static void s5k4ecgx_free_gpios(struct s5k4ecgx *priv)
  730. {
  731. int i;
  732. for (i = 0; i < ARRAY_SIZE(priv->gpio); i++) {
  733. if (!gpio_is_valid(priv->gpio[i].gpio))
  734. continue;
  735. gpio_free(priv->gpio[i].gpio);
  736. priv->gpio[i].gpio = -EINVAL;
  737. }
  738. }
  739. static int s5k4ecgx_config_gpios(struct s5k4ecgx *priv,
  740. const struct s5k4ecgx_platform_data *pdata)
  741. {
  742. const struct s5k4ecgx_gpio *gpio = &pdata->gpio_stby;
  743. int ret;
  744. priv->gpio[STBY].gpio = -EINVAL;
  745. priv->gpio[RST].gpio = -EINVAL;
  746. ret = s5k4ecgx_config_gpio(gpio->gpio, gpio->level, "S5K4ECGX_STBY");
  747. if (ret) {
  748. s5k4ecgx_free_gpios(priv);
  749. return ret;
  750. }
  751. priv->gpio[STBY] = *gpio;
  752. if (gpio_is_valid(gpio->gpio))
  753. gpio_set_value(gpio->gpio, 0);
  754. gpio = &pdata->gpio_reset;
  755. ret = s5k4ecgx_config_gpio(gpio->gpio, gpio->level, "S5K4ECGX_RST");
  756. if (ret) {
  757. s5k4ecgx_free_gpios(priv);
  758. return ret;
  759. }
  760. priv->gpio[RST] = *gpio;
  761. if (gpio_is_valid(gpio->gpio))
  762. gpio_set_value(gpio->gpio, 0);
  763. return 0;
  764. }
  765. static int s5k4ecgx_init_v4l2_ctrls(struct s5k4ecgx *priv)
  766. {
  767. const struct v4l2_ctrl_ops *ops = &s5k4ecgx_ctrl_ops;
  768. struct v4l2_ctrl_handler *hdl = &priv->handler;
  769. int ret;
  770. ret = v4l2_ctrl_handler_init(hdl, 4);
  771. if (ret)
  772. return ret;
  773. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS, -208, 127, 1, 0);
  774. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, -127, 127, 1, 0);
  775. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, -127, 127, 1, 0);
  776. /* Sharpness default is 24612, and then (24612/SHARPNESS_DIV) = 2 */
  777. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SHARPNESS, -32704/SHARPNESS_DIV,
  778. 24612/SHARPNESS_DIV, 1, 2);
  779. if (hdl->error) {
  780. ret = hdl->error;
  781. v4l2_ctrl_handler_free(hdl);
  782. return ret;
  783. }
  784. priv->sd.ctrl_handler = hdl;
  785. return 0;
  786. };
  787. static int s5k4ecgx_probe(struct i2c_client *client,
  788. const struct i2c_device_id *id)
  789. {
  790. struct s5k4ecgx_platform_data *pdata = client->dev.platform_data;
  791. struct v4l2_subdev *sd;
  792. struct s5k4ecgx *priv;
  793. int ret, i;
  794. if (pdata == NULL) {
  795. dev_err(&client->dev, "platform data is missing!\n");
  796. return -EINVAL;
  797. }
  798. priv = devm_kzalloc(&client->dev, sizeof(struct s5k4ecgx), GFP_KERNEL);
  799. if (!priv)
  800. return -ENOMEM;
  801. mutex_init(&priv->lock);
  802. priv->streaming = 0;
  803. sd = &priv->sd;
  804. /* Registering subdev */
  805. v4l2_i2c_subdev_init(sd, client, &s5k4ecgx_ops);
  806. strlcpy(sd->name, S5K4ECGX_DRIVER_NAME, sizeof(sd->name));
  807. sd->internal_ops = &s5k4ecgx_subdev_internal_ops;
  808. /* Support v4l2 sub-device user space API */
  809. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  810. priv->pad.flags = MEDIA_PAD_FL_SOURCE;
  811. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
  812. ret = media_entity_init(&sd->entity, 1, &priv->pad, 0);
  813. if (ret)
  814. return ret;
  815. ret = s5k4ecgx_config_gpios(priv, pdata);
  816. if (ret) {
  817. dev_err(&client->dev, "Failed to set gpios\n");
  818. goto out_err1;
  819. }
  820. for (i = 0; i < S5K4ECGX_NUM_SUPPLIES; i++)
  821. priv->supplies[i].supply = s5k4ecgx_supply_names[i];
  822. ret = devm_regulator_bulk_get(&client->dev, S5K4ECGX_NUM_SUPPLIES,
  823. priv->supplies);
  824. if (ret) {
  825. dev_err(&client->dev, "Failed to get regulators\n");
  826. goto out_err2;
  827. }
  828. ret = s5k4ecgx_init_v4l2_ctrls(priv);
  829. if (ret)
  830. goto out_err2;
  831. priv->curr_pixfmt = &s5k4ecgx_formats[0];
  832. priv->curr_frmsize = &s5k4ecgx_prev_sizes[0];
  833. return 0;
  834. out_err2:
  835. s5k4ecgx_free_gpios(priv);
  836. out_err1:
  837. media_entity_cleanup(&priv->sd.entity);
  838. return ret;
  839. }
  840. static int s5k4ecgx_remove(struct i2c_client *client)
  841. {
  842. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  843. struct s5k4ecgx *priv = to_s5k4ecgx(sd);
  844. mutex_destroy(&priv->lock);
  845. s5k4ecgx_free_gpios(priv);
  846. v4l2_device_unregister_subdev(sd);
  847. v4l2_ctrl_handler_free(&priv->handler);
  848. media_entity_cleanup(&sd->entity);
  849. return 0;
  850. }
  851. static const struct i2c_device_id s5k4ecgx_id[] = {
  852. { S5K4ECGX_DRIVER_NAME, 0 },
  853. {}
  854. };
  855. MODULE_DEVICE_TABLE(i2c, s5k4ecgx_id);
  856. static struct i2c_driver v4l2_i2c_driver = {
  857. .driver = {
  858. .owner = THIS_MODULE,
  859. .name = S5K4ECGX_DRIVER_NAME,
  860. },
  861. .probe = s5k4ecgx_probe,
  862. .remove = s5k4ecgx_remove,
  863. .id_table = s5k4ecgx_id,
  864. };
  865. module_i2c_driver(v4l2_i2c_driver);
  866. MODULE_DESCRIPTION("Samsung S5K4ECGX 5MP SOC camera");
  867. MODULE_AUTHOR("Sangwook Lee <sangwook.lee@linaro.org>");
  868. MODULE_AUTHOR("Seok-Young Jang <quartz.jang@samsung.com>");
  869. MODULE_LICENSE("GPL");
  870. MODULE_FIRMWARE(S5K4ECGX_FIRMWARE);