m5mols_core.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /*
  2. * Driver for M-5MOLS 8M Pixel camera sensor with ISP
  3. *
  4. * Copyright (C) 2011 Samsung Electronics Co., Ltd.
  5. * Author: HeungJun Kim <riverful.kim@samsung.com>
  6. *
  7. * Copyright (C) 2009 Samsung Electronics Co., Ltd.
  8. * Author: Dongsoo Nathaniel Kim <dongsoo45.kim@samsung.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. #include <linux/i2c.h>
  16. #include <linux/slab.h>
  17. #include <linux/irq.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/delay.h>
  20. #include <linux/gpio.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/videodev2.h>
  23. #include <linux/module.h>
  24. #include <media/v4l2-ctrls.h>
  25. #include <media/v4l2-device.h>
  26. #include <media/v4l2-subdev.h>
  27. #include <media/m5mols.h>
  28. #include "m5mols.h"
  29. #include "m5mols_reg.h"
  30. int m5mols_debug;
  31. module_param(m5mols_debug, int, 0644);
  32. #define MODULE_NAME "M5MOLS"
  33. #define M5MOLS_I2C_CHECK_RETRY 500
  34. /* The regulator consumer names for external voltage regulators */
  35. static struct regulator_bulk_data supplies[] = {
  36. {
  37. .supply = "core", /* ARM core power, 1.2V */
  38. }, {
  39. .supply = "dig_18", /* digital power 1, 1.8V */
  40. }, {
  41. .supply = "d_sensor", /* sensor power 1, 1.8V */
  42. }, {
  43. .supply = "dig_28", /* digital power 2, 2.8V */
  44. }, {
  45. .supply = "a_sensor", /* analog power */
  46. }, {
  47. .supply = "dig_12", /* digital power 3, 1.2V */
  48. },
  49. };
  50. static struct v4l2_mbus_framefmt m5mols_default_ffmt[M5MOLS_RESTYPE_MAX] = {
  51. [M5MOLS_RESTYPE_MONITOR] = {
  52. .width = 1920,
  53. .height = 1080,
  54. .code = V4L2_MBUS_FMT_VYUY8_2X8,
  55. .field = V4L2_FIELD_NONE,
  56. .colorspace = V4L2_COLORSPACE_JPEG,
  57. },
  58. [M5MOLS_RESTYPE_CAPTURE] = {
  59. .width = 1920,
  60. .height = 1080,
  61. .code = V4L2_MBUS_FMT_JPEG_1X8,
  62. .field = V4L2_FIELD_NONE,
  63. .colorspace = V4L2_COLORSPACE_JPEG,
  64. },
  65. };
  66. #define SIZE_DEFAULT_FFMT ARRAY_SIZE(m5mols_default_ffmt)
  67. static const struct m5mols_resolution m5mols_reg_res[] = {
  68. { 0x01, M5MOLS_RESTYPE_MONITOR, 128, 96 }, /* SUB-QCIF */
  69. { 0x03, M5MOLS_RESTYPE_MONITOR, 160, 120 }, /* QQVGA */
  70. { 0x05, M5MOLS_RESTYPE_MONITOR, 176, 144 }, /* QCIF */
  71. { 0x06, M5MOLS_RESTYPE_MONITOR, 176, 176 },
  72. { 0x08, M5MOLS_RESTYPE_MONITOR, 240, 320 }, /* QVGA */
  73. { 0x09, M5MOLS_RESTYPE_MONITOR, 320, 240 }, /* QVGA */
  74. { 0x0c, M5MOLS_RESTYPE_MONITOR, 240, 400 }, /* WQVGA */
  75. { 0x0d, M5MOLS_RESTYPE_MONITOR, 400, 240 }, /* WQVGA */
  76. { 0x0e, M5MOLS_RESTYPE_MONITOR, 352, 288 }, /* CIF */
  77. { 0x13, M5MOLS_RESTYPE_MONITOR, 480, 360 },
  78. { 0x15, M5MOLS_RESTYPE_MONITOR, 640, 360 }, /* qHD */
  79. { 0x17, M5MOLS_RESTYPE_MONITOR, 640, 480 }, /* VGA */
  80. { 0x18, M5MOLS_RESTYPE_MONITOR, 720, 480 },
  81. { 0x1a, M5MOLS_RESTYPE_MONITOR, 800, 480 }, /* WVGA */
  82. { 0x1f, M5MOLS_RESTYPE_MONITOR, 800, 600 }, /* SVGA */
  83. { 0x21, M5MOLS_RESTYPE_MONITOR, 1280, 720 }, /* HD */
  84. { 0x25, M5MOLS_RESTYPE_MONITOR, 1920, 1080 }, /* 1080p */
  85. { 0x29, M5MOLS_RESTYPE_MONITOR, 3264, 2448 }, /* 2.63fps 8M */
  86. { 0x39, M5MOLS_RESTYPE_MONITOR, 800, 602 }, /* AHS_MON debug */
  87. { 0x02, M5MOLS_RESTYPE_CAPTURE, 320, 240 }, /* QVGA */
  88. { 0x04, M5MOLS_RESTYPE_CAPTURE, 400, 240 }, /* WQVGA */
  89. { 0x07, M5MOLS_RESTYPE_CAPTURE, 480, 360 },
  90. { 0x08, M5MOLS_RESTYPE_CAPTURE, 640, 360 }, /* qHD */
  91. { 0x09, M5MOLS_RESTYPE_CAPTURE, 640, 480 }, /* VGA */
  92. { 0x0a, M5MOLS_RESTYPE_CAPTURE, 800, 480 }, /* WVGA */
  93. { 0x10, M5MOLS_RESTYPE_CAPTURE, 1280, 720 }, /* HD */
  94. { 0x14, M5MOLS_RESTYPE_CAPTURE, 1280, 960 }, /* 1M */
  95. { 0x17, M5MOLS_RESTYPE_CAPTURE, 1600, 1200 }, /* 2M */
  96. { 0x19, M5MOLS_RESTYPE_CAPTURE, 1920, 1080 }, /* Full-HD */
  97. { 0x1a, M5MOLS_RESTYPE_CAPTURE, 2048, 1152 }, /* 3Mega */
  98. { 0x1b, M5MOLS_RESTYPE_CAPTURE, 2048, 1536 },
  99. { 0x1c, M5MOLS_RESTYPE_CAPTURE, 2560, 1440 }, /* 4Mega */
  100. { 0x1d, M5MOLS_RESTYPE_CAPTURE, 2560, 1536 },
  101. { 0x1f, M5MOLS_RESTYPE_CAPTURE, 2560, 1920 }, /* 5Mega */
  102. { 0x21, M5MOLS_RESTYPE_CAPTURE, 3264, 1836 }, /* 6Mega */
  103. { 0x22, M5MOLS_RESTYPE_CAPTURE, 3264, 1960 },
  104. { 0x25, M5MOLS_RESTYPE_CAPTURE, 3264, 2448 }, /* 8Mega */
  105. };
  106. /**
  107. * m5mols_swap_byte - an byte array to integer conversion function
  108. * @size: size in bytes of I2C packet defined in the M-5MOLS datasheet
  109. *
  110. * Convert I2C data byte array with performing any required byte
  111. * reordering to assure proper values for each data type, regardless
  112. * of the architecture endianness.
  113. */
  114. static u32 m5mols_swap_byte(u8 *data, u8 length)
  115. {
  116. if (length == 1)
  117. return *data;
  118. else if (length == 2)
  119. return be16_to_cpu(*((u16 *)data));
  120. else
  121. return be32_to_cpu(*((u32 *)data));
  122. }
  123. /**
  124. * m5mols_read - I2C read function
  125. * @reg: combination of size, category and command for the I2C packet
  126. * @size: desired size of I2C packet
  127. * @val: read value
  128. *
  129. * Returns 0 on success, or else negative errno.
  130. */
  131. static int m5mols_read(struct v4l2_subdev *sd, u32 size, u32 reg, u32 *val)
  132. {
  133. struct i2c_client *client = v4l2_get_subdevdata(sd);
  134. struct m5mols_info *info = to_m5mols(sd);
  135. u8 rbuf[M5MOLS_I2C_MAX_SIZE + 1];
  136. u8 category = I2C_CATEGORY(reg);
  137. u8 cmd = I2C_COMMAND(reg);
  138. struct i2c_msg msg[2];
  139. u8 wbuf[5];
  140. int ret;
  141. if (!client->adapter)
  142. return -ENODEV;
  143. msg[0].addr = client->addr;
  144. msg[0].flags = 0;
  145. msg[0].len = 5;
  146. msg[0].buf = wbuf;
  147. wbuf[0] = 5;
  148. wbuf[1] = M5MOLS_BYTE_READ;
  149. wbuf[2] = category;
  150. wbuf[3] = cmd;
  151. wbuf[4] = size;
  152. msg[1].addr = client->addr;
  153. msg[1].flags = I2C_M_RD;
  154. msg[1].len = size + 1;
  155. msg[1].buf = rbuf;
  156. /* minimum stabilization time */
  157. usleep_range(200, 200);
  158. ret = i2c_transfer(client->adapter, msg, 2);
  159. if (ret == 2) {
  160. *val = m5mols_swap_byte(&rbuf[1], size);
  161. return 0;
  162. }
  163. if (info->isp_ready)
  164. v4l2_err(sd, "read failed: size:%d cat:%02x cmd:%02x. %d\n",
  165. size, category, cmd, ret);
  166. return ret < 0 ? ret : -EIO;
  167. }
  168. int m5mols_read_u8(struct v4l2_subdev *sd, u32 reg, u8 *val)
  169. {
  170. u32 val_32;
  171. int ret;
  172. if (I2C_SIZE(reg) != 1) {
  173. v4l2_err(sd, "Wrong data size\n");
  174. return -EINVAL;
  175. }
  176. ret = m5mols_read(sd, I2C_SIZE(reg), reg, &val_32);
  177. if (ret)
  178. return ret;
  179. *val = (u8)val_32;
  180. return ret;
  181. }
  182. int m5mols_read_u16(struct v4l2_subdev *sd, u32 reg, u16 *val)
  183. {
  184. u32 val_32;
  185. int ret;
  186. if (I2C_SIZE(reg) != 2) {
  187. v4l2_err(sd, "Wrong data size\n");
  188. return -EINVAL;
  189. }
  190. ret = m5mols_read(sd, I2C_SIZE(reg), reg, &val_32);
  191. if (ret)
  192. return ret;
  193. *val = (u16)val_32;
  194. return ret;
  195. }
  196. int m5mols_read_u32(struct v4l2_subdev *sd, u32 reg, u32 *val)
  197. {
  198. if (I2C_SIZE(reg) != 4) {
  199. v4l2_err(sd, "Wrong data size\n");
  200. return -EINVAL;
  201. }
  202. return m5mols_read(sd, I2C_SIZE(reg), reg, val);
  203. }
  204. /**
  205. * m5mols_write - I2C command write function
  206. * @reg: combination of size, category and command for the I2C packet
  207. * @val: value to write
  208. *
  209. * Returns 0 on success, or else negative errno.
  210. */
  211. int m5mols_write(struct v4l2_subdev *sd, u32 reg, u32 val)
  212. {
  213. struct i2c_client *client = v4l2_get_subdevdata(sd);
  214. struct m5mols_info *info = to_m5mols(sd);
  215. u8 wbuf[M5MOLS_I2C_MAX_SIZE + 4];
  216. u8 category = I2C_CATEGORY(reg);
  217. u8 cmd = I2C_COMMAND(reg);
  218. u8 size = I2C_SIZE(reg);
  219. u32 *buf = (u32 *)&wbuf[4];
  220. struct i2c_msg msg[1];
  221. int ret;
  222. if (!client->adapter)
  223. return -ENODEV;
  224. if (size != 1 && size != 2 && size != 4) {
  225. v4l2_err(sd, "Wrong data size\n");
  226. return -EINVAL;
  227. }
  228. msg->addr = client->addr;
  229. msg->flags = 0;
  230. msg->len = (u16)size + 4;
  231. msg->buf = wbuf;
  232. wbuf[0] = size + 4;
  233. wbuf[1] = M5MOLS_BYTE_WRITE;
  234. wbuf[2] = category;
  235. wbuf[3] = cmd;
  236. *buf = m5mols_swap_byte((u8 *)&val, size);
  237. usleep_range(200, 200);
  238. ret = i2c_transfer(client->adapter, msg, 1);
  239. if (ret == 1)
  240. return 0;
  241. if (info->isp_ready)
  242. v4l2_err(sd, "write failed: cat:%02x cmd:%02x ret:%d\n",
  243. category, cmd, ret);
  244. return ret < 0 ? ret : -EIO;
  245. }
  246. /**
  247. * m5mols_busy_wait - Busy waiting with I2C register polling
  248. * @reg: the I2C_REG() address of an 8-bit status register to check
  249. * @value: expected status register value
  250. * @mask: bit mask for the read status register value
  251. * @timeout: timeout in miliseconds, or -1 for default timeout
  252. *
  253. * The @reg register value is ORed with @mask before comparing with @value.
  254. *
  255. * Return: 0 if the requested condition became true within less than
  256. * @timeout ms, or else negative errno.
  257. */
  258. int m5mols_busy_wait(struct v4l2_subdev *sd, u32 reg, u32 value, u32 mask,
  259. int timeout)
  260. {
  261. int ms = timeout < 0 ? M5MOLS_BUSY_WAIT_DEF_TIMEOUT : timeout;
  262. unsigned long end = jiffies + msecs_to_jiffies(ms);
  263. u8 status;
  264. do {
  265. int ret = m5mols_read_u8(sd, reg, &status);
  266. if (ret < 0 && !(mask & M5MOLS_I2C_RDY_WAIT_FL))
  267. return ret;
  268. if (!ret && (status & mask & 0xff) == (value & 0xff))
  269. return 0;
  270. usleep_range(100, 250);
  271. } while (ms > 0 && time_is_after_jiffies(end));
  272. return -EBUSY;
  273. }
  274. /**
  275. * m5mols_enable_interrupt - Clear interrupt pending bits and unmask interrupts
  276. *
  277. * Before writing desired interrupt value the INT_FACTOR register should
  278. * be read to clear pending interrupts.
  279. */
  280. int m5mols_enable_interrupt(struct v4l2_subdev *sd, u8 reg)
  281. {
  282. struct m5mols_info *info = to_m5mols(sd);
  283. u8 mask = is_available_af(info) ? REG_INT_AF : 0;
  284. u8 dummy;
  285. int ret;
  286. ret = m5mols_read_u8(sd, SYSTEM_INT_FACTOR, &dummy);
  287. if (!ret)
  288. ret = m5mols_write(sd, SYSTEM_INT_ENABLE, reg & ~mask);
  289. return ret;
  290. }
  291. int m5mols_wait_interrupt(struct v4l2_subdev *sd, u8 irq_mask, u32 timeout)
  292. {
  293. struct m5mols_info *info = to_m5mols(sd);
  294. int ret = wait_event_interruptible_timeout(info->irq_waitq,
  295. atomic_add_unless(&info->irq_done, -1, 0),
  296. msecs_to_jiffies(timeout));
  297. if (ret <= 0)
  298. return ret ? ret : -ETIMEDOUT;
  299. return m5mols_busy_wait(sd, SYSTEM_INT_FACTOR, irq_mask,
  300. M5MOLS_I2C_RDY_WAIT_FL | irq_mask, -1);
  301. }
  302. /**
  303. * m5mols_reg_mode - Write the mode and check busy status
  304. *
  305. * It always accompanies a little delay changing the M-5MOLS mode, so it is
  306. * needed checking current busy status to guarantee right mode.
  307. */
  308. static int m5mols_reg_mode(struct v4l2_subdev *sd, u8 mode)
  309. {
  310. int ret = m5mols_write(sd, SYSTEM_SYSMODE, mode);
  311. if (ret < 0)
  312. return ret;
  313. return m5mols_busy_wait(sd, SYSTEM_SYSMODE, mode, 0xff,
  314. M5MOLS_MODE_CHANGE_TIMEOUT);
  315. }
  316. /**
  317. * m5mols_mode - manage the M-5MOLS's mode
  318. * @mode: the required operation mode
  319. *
  320. * The commands of M-5MOLS are grouped into specific modes. Each functionality
  321. * can be guaranteed only when the sensor is operating in mode which which
  322. * a command belongs to.
  323. */
  324. int m5mols_mode(struct m5mols_info *info, u8 mode)
  325. {
  326. struct v4l2_subdev *sd = &info->sd;
  327. int ret = -EINVAL;
  328. u8 reg;
  329. if (mode < REG_PARAMETER || mode > REG_CAPTURE)
  330. return ret;
  331. ret = m5mols_read_u8(sd, SYSTEM_SYSMODE, &reg);
  332. if (ret || reg == mode)
  333. return ret;
  334. switch (reg) {
  335. case REG_PARAMETER:
  336. ret = m5mols_reg_mode(sd, REG_MONITOR);
  337. if (mode == REG_MONITOR)
  338. break;
  339. if (!ret)
  340. ret = m5mols_reg_mode(sd, REG_CAPTURE);
  341. break;
  342. case REG_MONITOR:
  343. if (mode == REG_PARAMETER) {
  344. ret = m5mols_reg_mode(sd, REG_PARAMETER);
  345. break;
  346. }
  347. ret = m5mols_reg_mode(sd, REG_CAPTURE);
  348. break;
  349. case REG_CAPTURE:
  350. ret = m5mols_reg_mode(sd, REG_MONITOR);
  351. if (mode == REG_MONITOR)
  352. break;
  353. if (!ret)
  354. ret = m5mols_reg_mode(sd, REG_PARAMETER);
  355. break;
  356. default:
  357. v4l2_warn(sd, "Wrong mode: %d\n", mode);
  358. }
  359. if (!ret)
  360. info->mode = mode;
  361. return ret;
  362. }
  363. /**
  364. * m5mols_get_version - retrieve full revisions information of M-5MOLS
  365. *
  366. * The version information includes revisions of hardware and firmware,
  367. * AutoFocus alghorithm version and the version string.
  368. */
  369. static int m5mols_get_version(struct v4l2_subdev *sd)
  370. {
  371. struct m5mols_info *info = to_m5mols(sd);
  372. struct m5mols_version *ver = &info->ver;
  373. u8 *str = ver->str;
  374. int i;
  375. int ret;
  376. ret = m5mols_read_u8(sd, SYSTEM_VER_CUSTOMER, &ver->customer);
  377. if (!ret)
  378. ret = m5mols_read_u8(sd, SYSTEM_VER_PROJECT, &ver->project);
  379. if (!ret)
  380. ret = m5mols_read_u16(sd, SYSTEM_VER_FIRMWARE, &ver->fw);
  381. if (!ret)
  382. ret = m5mols_read_u16(sd, SYSTEM_VER_HARDWARE, &ver->hw);
  383. if (!ret)
  384. ret = m5mols_read_u16(sd, SYSTEM_VER_PARAMETER, &ver->param);
  385. if (!ret)
  386. ret = m5mols_read_u16(sd, SYSTEM_VER_AWB, &ver->awb);
  387. if (!ret)
  388. ret = m5mols_read_u8(sd, AF_VERSION, &ver->af);
  389. if (ret)
  390. return ret;
  391. for (i = 0; i < VERSION_STRING_SIZE; i++) {
  392. ret = m5mols_read_u8(sd, SYSTEM_VER_STRING, &str[i]);
  393. if (ret)
  394. return ret;
  395. }
  396. ver->fw = be16_to_cpu(ver->fw);
  397. ver->hw = be16_to_cpu(ver->hw);
  398. ver->param = be16_to_cpu(ver->param);
  399. ver->awb = be16_to_cpu(ver->awb);
  400. v4l2_info(sd, "Manufacturer\t[%s]\n",
  401. is_manufacturer(info, REG_SAMSUNG_ELECTRO) ?
  402. "Samsung Electro-Machanics" :
  403. is_manufacturer(info, REG_SAMSUNG_OPTICS) ?
  404. "Samsung Fiber-Optics" :
  405. is_manufacturer(info, REG_SAMSUNG_TECHWIN) ?
  406. "Samsung Techwin" : "None");
  407. v4l2_info(sd, "Customer/Project\t[0x%02x/0x%02x]\n",
  408. info->ver.customer, info->ver.project);
  409. if (!is_available_af(info))
  410. v4l2_info(sd, "No support Auto Focus on this firmware\n");
  411. return ret;
  412. }
  413. /**
  414. * __find_restype - Lookup M-5MOLS resolution type according to pixel code
  415. * @code: pixel code
  416. */
  417. static enum m5mols_restype __find_restype(enum v4l2_mbus_pixelcode code)
  418. {
  419. enum m5mols_restype type = M5MOLS_RESTYPE_MONITOR;
  420. do {
  421. if (code == m5mols_default_ffmt[type].code)
  422. return type;
  423. } while (type++ != SIZE_DEFAULT_FFMT);
  424. return 0;
  425. }
  426. /**
  427. * __find_resolution - Lookup preset and type of M-5MOLS's resolution
  428. * @mf: pixel format to find/negotiate the resolution preset for
  429. * @type: M-5MOLS resolution type
  430. * @resolution: M-5MOLS resolution preset register value
  431. *
  432. * Find nearest resolution matching resolution preset and adjust mf
  433. * to supported values.
  434. */
  435. static int __find_resolution(struct v4l2_subdev *sd,
  436. struct v4l2_mbus_framefmt *mf,
  437. enum m5mols_restype *type,
  438. u32 *resolution)
  439. {
  440. const struct m5mols_resolution *fsize = &m5mols_reg_res[0];
  441. const struct m5mols_resolution *match = NULL;
  442. enum m5mols_restype stype = __find_restype(mf->code);
  443. int i = ARRAY_SIZE(m5mols_reg_res);
  444. unsigned int min_err = ~0;
  445. while (i--) {
  446. int err;
  447. if (stype == fsize->type) {
  448. err = abs(fsize->width - mf->width)
  449. + abs(fsize->height - mf->height);
  450. if (err < min_err) {
  451. min_err = err;
  452. match = fsize;
  453. }
  454. }
  455. fsize++;
  456. }
  457. if (match) {
  458. mf->width = match->width;
  459. mf->height = match->height;
  460. *resolution = match->reg;
  461. *type = stype;
  462. return 0;
  463. }
  464. return -EINVAL;
  465. }
  466. static struct v4l2_mbus_framefmt *__find_format(struct m5mols_info *info,
  467. struct v4l2_subdev_fh *fh,
  468. enum v4l2_subdev_format_whence which,
  469. enum m5mols_restype type)
  470. {
  471. if (which == V4L2_SUBDEV_FORMAT_TRY)
  472. return fh ? v4l2_subdev_get_try_format(fh, 0) : NULL;
  473. return &info->ffmt[type];
  474. }
  475. static int m5mols_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  476. struct v4l2_subdev_format *fmt)
  477. {
  478. struct m5mols_info *info = to_m5mols(sd);
  479. struct v4l2_mbus_framefmt *format;
  480. format = __find_format(info, fh, fmt->which, info->res_type);
  481. if (!format)
  482. return -EINVAL;
  483. fmt->format = *format;
  484. return 0;
  485. }
  486. static int m5mols_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  487. struct v4l2_subdev_format *fmt)
  488. {
  489. struct m5mols_info *info = to_m5mols(sd);
  490. struct v4l2_mbus_framefmt *format = &fmt->format;
  491. struct v4l2_mbus_framefmt *sfmt;
  492. enum m5mols_restype type;
  493. u32 resolution = 0;
  494. int ret;
  495. ret = __find_resolution(sd, format, &type, &resolution);
  496. if (ret < 0)
  497. return ret;
  498. sfmt = __find_format(info, fh, fmt->which, type);
  499. if (!sfmt)
  500. return 0;
  501. format->code = m5mols_default_ffmt[type].code;
  502. format->colorspace = V4L2_COLORSPACE_JPEG;
  503. format->field = V4L2_FIELD_NONE;
  504. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  505. *sfmt = *format;
  506. info->resolution = resolution;
  507. info->res_type = type;
  508. }
  509. return 0;
  510. }
  511. static int m5mols_enum_mbus_code(struct v4l2_subdev *sd,
  512. struct v4l2_subdev_fh *fh,
  513. struct v4l2_subdev_mbus_code_enum *code)
  514. {
  515. if (!code || code->index >= SIZE_DEFAULT_FFMT)
  516. return -EINVAL;
  517. code->code = m5mols_default_ffmt[code->index].code;
  518. return 0;
  519. }
  520. static struct v4l2_subdev_pad_ops m5mols_pad_ops = {
  521. .enum_mbus_code = m5mols_enum_mbus_code,
  522. .get_fmt = m5mols_get_fmt,
  523. .set_fmt = m5mols_set_fmt,
  524. };
  525. /**
  526. * m5mols_restore_controls - Apply current control values to the registers
  527. *
  528. * m5mols_do_scenemode() handles all parameters for which there is yet no
  529. * individual control. It should be replaced at some point by setting each
  530. * control individually, in required register set up order.
  531. */
  532. int m5mols_restore_controls(struct m5mols_info *info)
  533. {
  534. int ret;
  535. if (info->ctrl_sync)
  536. return 0;
  537. ret = m5mols_do_scenemode(info, REG_SCENE_NORMAL);
  538. if (ret)
  539. return ret;
  540. ret = v4l2_ctrl_handler_setup(&info->handle);
  541. info->ctrl_sync = !ret;
  542. return ret;
  543. }
  544. /**
  545. * m5mols_start_monitor - Start the monitor mode
  546. *
  547. * Before applying the controls setup the resolution and frame rate
  548. * in PARAMETER mode, and then switch over to MONITOR mode.
  549. */
  550. static int m5mols_start_monitor(struct m5mols_info *info)
  551. {
  552. struct v4l2_subdev *sd = &info->sd;
  553. int ret;
  554. ret = m5mols_mode(info, REG_PARAMETER);
  555. if (!ret)
  556. ret = m5mols_write(sd, PARM_MON_SIZE, info->resolution);
  557. if (!ret)
  558. ret = m5mols_write(sd, PARM_MON_FPS, REG_FPS_30);
  559. if (!ret)
  560. ret = m5mols_mode(info, REG_MONITOR);
  561. if (!ret)
  562. ret = m5mols_restore_controls(info);
  563. return ret;
  564. }
  565. static int m5mols_s_stream(struct v4l2_subdev *sd, int enable)
  566. {
  567. struct m5mols_info *info = to_m5mols(sd);
  568. u32 code = info->ffmt[info->res_type].code;
  569. if (enable) {
  570. int ret = -EINVAL;
  571. if (is_code(code, M5MOLS_RESTYPE_MONITOR))
  572. ret = m5mols_start_monitor(info);
  573. if (is_code(code, M5MOLS_RESTYPE_CAPTURE))
  574. ret = m5mols_start_capture(info);
  575. return ret;
  576. }
  577. return m5mols_mode(info, REG_PARAMETER);
  578. }
  579. static const struct v4l2_subdev_video_ops m5mols_video_ops = {
  580. .s_stream = m5mols_s_stream,
  581. };
  582. static int m5mols_s_ctrl(struct v4l2_ctrl *ctrl)
  583. {
  584. struct v4l2_subdev *sd = to_sd(ctrl);
  585. struct m5mols_info *info = to_m5mols(sd);
  586. int ispstate = info->mode;
  587. int ret;
  588. /*
  589. * If needed, defer restoring the controls until
  590. * the device is fully initialized.
  591. */
  592. if (!info->isp_ready) {
  593. info->ctrl_sync = 0;
  594. return 0;
  595. }
  596. ret = m5mols_mode(info, REG_PARAMETER);
  597. if (ret < 0)
  598. return ret;
  599. ret = m5mols_set_ctrl(ctrl);
  600. if (ret < 0)
  601. return ret;
  602. return m5mols_mode(info, ispstate);
  603. }
  604. static const struct v4l2_ctrl_ops m5mols_ctrl_ops = {
  605. .s_ctrl = m5mols_s_ctrl,
  606. };
  607. static int m5mols_sensor_power(struct m5mols_info *info, bool enable)
  608. {
  609. struct v4l2_subdev *sd = &info->sd;
  610. struct i2c_client *client = v4l2_get_subdevdata(sd);
  611. const struct m5mols_platform_data *pdata = info->pdata;
  612. int ret;
  613. if (info->power == enable)
  614. return 0;
  615. if (enable) {
  616. if (info->set_power) {
  617. ret = info->set_power(&client->dev, 1);
  618. if (ret)
  619. return ret;
  620. }
  621. ret = regulator_bulk_enable(ARRAY_SIZE(supplies), supplies);
  622. if (ret) {
  623. info->set_power(&client->dev, 0);
  624. return ret;
  625. }
  626. gpio_set_value(pdata->gpio_reset, !pdata->reset_polarity);
  627. info->power = 1;
  628. return ret;
  629. }
  630. ret = regulator_bulk_disable(ARRAY_SIZE(supplies), supplies);
  631. if (ret)
  632. return ret;
  633. if (info->set_power)
  634. info->set_power(&client->dev, 0);
  635. gpio_set_value(pdata->gpio_reset, pdata->reset_polarity);
  636. info->isp_ready = 0;
  637. info->power = 0;
  638. return ret;
  639. }
  640. /* m5mols_update_fw - optional firmware update routine */
  641. int __attribute__ ((weak)) m5mols_update_fw(struct v4l2_subdev *sd,
  642. int (*set_power)(struct m5mols_info *, bool))
  643. {
  644. return 0;
  645. }
  646. /**
  647. * m5mols_fw_start - M-5MOLS internal ARM controller initialization
  648. *
  649. * Execute the M-5MOLS internal ARM controller initialization sequence.
  650. * This function should be called after the supply voltage has been
  651. * applied and before any requests to the device are made.
  652. */
  653. static int m5mols_fw_start(struct v4l2_subdev *sd)
  654. {
  655. struct m5mols_info *info = to_m5mols(sd);
  656. int ret;
  657. atomic_set(&info->irq_done, 0);
  658. /* Wait until I2C slave is initialized in Flash Writer mode */
  659. ret = m5mols_busy_wait(sd, FLASH_CAM_START, REG_IN_FLASH_MODE,
  660. M5MOLS_I2C_RDY_WAIT_FL | 0xff, -1);
  661. if (!ret)
  662. ret = m5mols_write(sd, FLASH_CAM_START, REG_START_ARM_BOOT);
  663. if (!ret)
  664. ret = m5mols_wait_interrupt(sd, REG_INT_MODE, 2000);
  665. if (ret < 0)
  666. return ret;
  667. info->isp_ready = 1;
  668. ret = m5mols_get_version(sd);
  669. if (!ret)
  670. ret = m5mols_update_fw(sd, m5mols_sensor_power);
  671. if (ret)
  672. return ret;
  673. v4l2_dbg(1, m5mols_debug, sd, "Success ARM Booting\n");
  674. ret = m5mols_write(sd, PARM_INTERFACE, REG_INTERFACE_MIPI);
  675. if (!ret)
  676. ret = m5mols_enable_interrupt(sd,
  677. REG_INT_AF | REG_INT_CAPTURE);
  678. return ret;
  679. }
  680. static int m5mols_init_controls(struct m5mols_info *info)
  681. {
  682. struct v4l2_subdev *sd = &info->sd;
  683. u16 max_exposure;
  684. u16 step_zoom;
  685. int ret;
  686. /* Determine value's range & step of controls for various FW version */
  687. ret = m5mols_read_u16(sd, AE_MAX_GAIN_MON, &max_exposure);
  688. if (!ret)
  689. step_zoom = is_manufacturer(info, REG_SAMSUNG_OPTICS) ? 31 : 1;
  690. if (ret)
  691. return ret;
  692. v4l2_ctrl_handler_init(&info->handle, 6);
  693. info->autowb = v4l2_ctrl_new_std(&info->handle,
  694. &m5mols_ctrl_ops, V4L2_CID_AUTO_WHITE_BALANCE,
  695. 0, 1, 1, 0);
  696. info->saturation = v4l2_ctrl_new_std(&info->handle,
  697. &m5mols_ctrl_ops, V4L2_CID_SATURATION,
  698. 1, 5, 1, 3);
  699. info->zoom = v4l2_ctrl_new_std(&info->handle,
  700. &m5mols_ctrl_ops, V4L2_CID_ZOOM_ABSOLUTE,
  701. 1, 70, step_zoom, 1);
  702. info->exposure = v4l2_ctrl_new_std(&info->handle,
  703. &m5mols_ctrl_ops, V4L2_CID_EXPOSURE,
  704. 0, max_exposure, 1, (int)max_exposure/2);
  705. info->colorfx = v4l2_ctrl_new_std_menu(&info->handle,
  706. &m5mols_ctrl_ops, V4L2_CID_COLORFX,
  707. 4, (1 << V4L2_COLORFX_BW), V4L2_COLORFX_NONE);
  708. info->autoexposure = v4l2_ctrl_new_std_menu(&info->handle,
  709. &m5mols_ctrl_ops, V4L2_CID_EXPOSURE_AUTO,
  710. 1, 0, V4L2_EXPOSURE_AUTO);
  711. sd->ctrl_handler = &info->handle;
  712. if (info->handle.error) {
  713. v4l2_err(sd, "Failed to initialize controls: %d\n", ret);
  714. v4l2_ctrl_handler_free(&info->handle);
  715. return info->handle.error;
  716. }
  717. v4l2_ctrl_cluster(2, &info->autoexposure);
  718. return 0;
  719. }
  720. /**
  721. * m5mols_s_power - Main sensor power control function
  722. *
  723. * To prevent breaking the lens when the sensor is powered off the Soft-Landing
  724. * algorithm is called where available. The Soft-Landing algorithm availability
  725. * dependends on the firmware provider.
  726. */
  727. static int m5mols_s_power(struct v4l2_subdev *sd, int on)
  728. {
  729. struct m5mols_info *info = to_m5mols(sd);
  730. int ret;
  731. if (on) {
  732. ret = m5mols_sensor_power(info, true);
  733. if (!ret)
  734. ret = m5mols_fw_start(sd);
  735. return ret;
  736. }
  737. if (is_manufacturer(info, REG_SAMSUNG_TECHWIN)) {
  738. ret = m5mols_mode(info, REG_MONITOR);
  739. if (!ret)
  740. ret = m5mols_write(sd, AF_EXECUTE, REG_AF_STOP);
  741. if (!ret)
  742. ret = m5mols_write(sd, AF_MODE, REG_AF_POWEROFF);
  743. if (!ret)
  744. ret = m5mols_busy_wait(sd, SYSTEM_STATUS, REG_AF_IDLE,
  745. 0xff, -1);
  746. if (ret < 0)
  747. v4l2_warn(sd, "Soft landing lens failed\n");
  748. }
  749. ret = m5mols_sensor_power(info, false);
  750. info->ctrl_sync = 0;
  751. return ret;
  752. }
  753. static int m5mols_log_status(struct v4l2_subdev *sd)
  754. {
  755. struct m5mols_info *info = to_m5mols(sd);
  756. v4l2_ctrl_handler_log_status(&info->handle, sd->name);
  757. return 0;
  758. }
  759. static const struct v4l2_subdev_core_ops m5mols_core_ops = {
  760. .s_power = m5mols_s_power,
  761. .g_ctrl = v4l2_subdev_g_ctrl,
  762. .s_ctrl = v4l2_subdev_s_ctrl,
  763. .queryctrl = v4l2_subdev_queryctrl,
  764. .querymenu = v4l2_subdev_querymenu,
  765. .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
  766. .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
  767. .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
  768. .log_status = m5mols_log_status,
  769. };
  770. /*
  771. * V4L2 subdev internal operations
  772. */
  773. static int m5mols_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  774. {
  775. struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(fh, 0);
  776. *format = m5mols_default_ffmt[0];
  777. return 0;
  778. }
  779. static const struct v4l2_subdev_internal_ops m5mols_subdev_internal_ops = {
  780. .open = m5mols_open,
  781. };
  782. static const struct v4l2_subdev_ops m5mols_ops = {
  783. .core = &m5mols_core_ops,
  784. .pad = &m5mols_pad_ops,
  785. .video = &m5mols_video_ops,
  786. };
  787. static irqreturn_t m5mols_irq_handler(int irq, void *data)
  788. {
  789. struct m5mols_info *info = to_m5mols(data);
  790. atomic_set(&info->irq_done, 1);
  791. wake_up_interruptible(&info->irq_waitq);
  792. return IRQ_HANDLED;
  793. }
  794. static int __devinit m5mols_probe(struct i2c_client *client,
  795. const struct i2c_device_id *id)
  796. {
  797. const struct m5mols_platform_data *pdata = client->dev.platform_data;
  798. struct m5mols_info *info;
  799. struct v4l2_subdev *sd;
  800. int ret;
  801. if (pdata == NULL) {
  802. dev_err(&client->dev, "No platform data\n");
  803. return -EINVAL;
  804. }
  805. if (!gpio_is_valid(pdata->gpio_reset)) {
  806. dev_err(&client->dev, "No valid RESET GPIO specified\n");
  807. return -EINVAL;
  808. }
  809. if (!client->irq) {
  810. dev_err(&client->dev, "Interrupt not assigned\n");
  811. return -EINVAL;
  812. }
  813. info = kzalloc(sizeof(struct m5mols_info), GFP_KERNEL);
  814. if (!info)
  815. return -ENOMEM;
  816. info->pdata = pdata;
  817. info->set_power = pdata->set_power;
  818. ret = gpio_request(pdata->gpio_reset, "M5MOLS_NRST");
  819. if (ret) {
  820. dev_err(&client->dev, "Failed to request gpio: %d\n", ret);
  821. goto out_free;
  822. }
  823. gpio_direction_output(pdata->gpio_reset, pdata->reset_polarity);
  824. ret = regulator_bulk_get(&client->dev, ARRAY_SIZE(supplies), supplies);
  825. if (ret) {
  826. dev_err(&client->dev, "Failed to get regulators: %d\n", ret);
  827. goto out_gpio;
  828. }
  829. sd = &info->sd;
  830. v4l2_i2c_subdev_init(sd, client, &m5mols_ops);
  831. strlcpy(sd->name, MODULE_NAME, sizeof(sd->name));
  832. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  833. sd->internal_ops = &m5mols_subdev_internal_ops;
  834. info->pad.flags = MEDIA_PAD_FL_SOURCE;
  835. ret = media_entity_init(&sd->entity, 1, &info->pad, 0);
  836. if (ret < 0)
  837. goto out_reg;
  838. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
  839. init_waitqueue_head(&info->irq_waitq);
  840. ret = request_irq(client->irq, m5mols_irq_handler,
  841. IRQF_TRIGGER_RISING, MODULE_NAME, sd);
  842. if (ret) {
  843. dev_err(&client->dev, "Interrupt request failed: %d\n", ret);
  844. goto out_me;
  845. }
  846. info->res_type = M5MOLS_RESTYPE_MONITOR;
  847. info->ffmt[0] = m5mols_default_ffmt[0];
  848. info->ffmt[1] = m5mols_default_ffmt[1];
  849. ret = m5mols_sensor_power(info, true);
  850. if (ret)
  851. goto out_me;
  852. ret = m5mols_fw_start(sd);
  853. if (!ret)
  854. ret = m5mols_init_controls(info);
  855. m5mols_sensor_power(info, false);
  856. if (!ret)
  857. return 0;
  858. out_me:
  859. media_entity_cleanup(&sd->entity);
  860. out_reg:
  861. regulator_bulk_free(ARRAY_SIZE(supplies), supplies);
  862. out_gpio:
  863. gpio_free(pdata->gpio_reset);
  864. out_free:
  865. kfree(info);
  866. return ret;
  867. }
  868. static int __devexit m5mols_remove(struct i2c_client *client)
  869. {
  870. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  871. struct m5mols_info *info = to_m5mols(sd);
  872. v4l2_device_unregister_subdev(sd);
  873. v4l2_ctrl_handler_free(sd->ctrl_handler);
  874. free_irq(client->irq, sd);
  875. regulator_bulk_free(ARRAY_SIZE(supplies), supplies);
  876. gpio_free(info->pdata->gpio_reset);
  877. media_entity_cleanup(&sd->entity);
  878. kfree(info);
  879. return 0;
  880. }
  881. static const struct i2c_device_id m5mols_id[] = {
  882. { MODULE_NAME, 0 },
  883. { },
  884. };
  885. MODULE_DEVICE_TABLE(i2c, m5mols_id);
  886. static struct i2c_driver m5mols_i2c_driver = {
  887. .driver = {
  888. .name = MODULE_NAME,
  889. },
  890. .probe = m5mols_probe,
  891. .remove = __devexit_p(m5mols_remove),
  892. .id_table = m5mols_id,
  893. };
  894. module_i2c_driver(m5mols_i2c_driver);
  895. MODULE_AUTHOR("HeungJun Kim <riverful.kim@samsung.com>");
  896. MODULE_AUTHOR("Dongsoo Kim <dongsoo45.kim@samsung.com>");
  897. MODULE_DESCRIPTION("Fujitsu M-5MOLS 8M Pixel camera driver");
  898. MODULE_LICENSE("GPL");