m5mols_core.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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_set_mode - set the M-5MOLS controller 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 a command
  322. * belongs to.
  323. */
  324. int m5mols_set_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. int ret = 0;
  481. mutex_lock(&info->lock);
  482. format = __find_format(info, fh, fmt->which, info->res_type);
  483. if (format)
  484. fmt->format = *format;
  485. else
  486. ret = -EINVAL;
  487. mutex_unlock(&info->lock);
  488. return ret;
  489. }
  490. static int m5mols_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  491. struct v4l2_subdev_format *fmt)
  492. {
  493. struct m5mols_info *info = to_m5mols(sd);
  494. struct v4l2_mbus_framefmt *format = &fmt->format;
  495. struct v4l2_mbus_framefmt *sfmt;
  496. enum m5mols_restype type;
  497. u32 resolution = 0;
  498. int ret;
  499. ret = __find_resolution(sd, format, &type, &resolution);
  500. if (ret < 0)
  501. return ret;
  502. sfmt = __find_format(info, fh, fmt->which, type);
  503. if (!sfmt)
  504. return 0;
  505. mutex_lock(&info->lock);
  506. format->code = m5mols_default_ffmt[type].code;
  507. format->colorspace = V4L2_COLORSPACE_JPEG;
  508. format->field = V4L2_FIELD_NONE;
  509. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  510. *sfmt = *format;
  511. info->resolution = resolution;
  512. info->res_type = type;
  513. }
  514. mutex_unlock(&info->lock);
  515. return ret;
  516. }
  517. static int m5mols_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
  518. struct v4l2_mbus_frame_desc *fd)
  519. {
  520. struct m5mols_info *info = to_m5mols(sd);
  521. if (pad != 0 || fd == NULL)
  522. return -EINVAL;
  523. mutex_lock(&info->lock);
  524. /*
  525. * .get_frame_desc is only used for compressed formats,
  526. * thus we always return the capture frame parameters here.
  527. */
  528. fd->entry[0].length = info->cap.buf_size;
  529. fd->entry[0].pixelcode = info->ffmt[M5MOLS_RESTYPE_CAPTURE].code;
  530. mutex_unlock(&info->lock);
  531. fd->entry[0].flags = V4L2_MBUS_FRAME_DESC_FL_LEN_MAX;
  532. fd->num_entries = 1;
  533. return 0;
  534. }
  535. static int m5mols_set_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
  536. struct v4l2_mbus_frame_desc *fd)
  537. {
  538. struct m5mols_info *info = to_m5mols(sd);
  539. struct v4l2_mbus_framefmt *mf = &info->ffmt[M5MOLS_RESTYPE_CAPTURE];
  540. if (pad != 0 || fd == NULL)
  541. return -EINVAL;
  542. fd->entry[0].flags = V4L2_MBUS_FRAME_DESC_FL_LEN_MAX;
  543. fd->num_entries = 1;
  544. fd->entry[0].length = clamp_t(u32, fd->entry[0].length,
  545. mf->width * mf->height,
  546. M5MOLS_MAIN_JPEG_SIZE_MAX);
  547. mutex_lock(&info->lock);
  548. info->cap.buf_size = fd->entry[0].length;
  549. mutex_unlock(&info->lock);
  550. return 0;
  551. }
  552. static int m5mols_enum_mbus_code(struct v4l2_subdev *sd,
  553. struct v4l2_subdev_fh *fh,
  554. struct v4l2_subdev_mbus_code_enum *code)
  555. {
  556. if (!code || code->index >= SIZE_DEFAULT_FFMT)
  557. return -EINVAL;
  558. code->code = m5mols_default_ffmt[code->index].code;
  559. return 0;
  560. }
  561. static struct v4l2_subdev_pad_ops m5mols_pad_ops = {
  562. .enum_mbus_code = m5mols_enum_mbus_code,
  563. .get_fmt = m5mols_get_fmt,
  564. .set_fmt = m5mols_set_fmt,
  565. .get_frame_desc = m5mols_get_frame_desc,
  566. .set_frame_desc = m5mols_set_frame_desc,
  567. };
  568. /**
  569. * m5mols_restore_controls - Apply current control values to the registers
  570. *
  571. * m5mols_do_scenemode() handles all parameters for which there is yet no
  572. * individual control. It should be replaced at some point by setting each
  573. * control individually, in required register set up order.
  574. */
  575. int m5mols_restore_controls(struct m5mols_info *info)
  576. {
  577. int ret;
  578. if (info->ctrl_sync)
  579. return 0;
  580. ret = m5mols_do_scenemode(info, REG_SCENE_NORMAL);
  581. if (ret)
  582. return ret;
  583. ret = v4l2_ctrl_handler_setup(&info->handle);
  584. info->ctrl_sync = !ret;
  585. return ret;
  586. }
  587. /**
  588. * m5mols_start_monitor - Start the monitor mode
  589. *
  590. * Before applying the controls setup the resolution and frame rate
  591. * in PARAMETER mode, and then switch over to MONITOR mode.
  592. */
  593. static int m5mols_start_monitor(struct m5mols_info *info)
  594. {
  595. struct v4l2_subdev *sd = &info->sd;
  596. int ret;
  597. ret = m5mols_set_mode(info, REG_PARAMETER);
  598. if (!ret)
  599. ret = m5mols_write(sd, PARM_MON_SIZE, info->resolution);
  600. if (!ret)
  601. ret = m5mols_write(sd, PARM_MON_FPS, REG_FPS_30);
  602. if (!ret)
  603. ret = m5mols_set_mode(info, REG_MONITOR);
  604. if (!ret)
  605. ret = m5mols_restore_controls(info);
  606. return ret;
  607. }
  608. static int m5mols_s_stream(struct v4l2_subdev *sd, int enable)
  609. {
  610. struct m5mols_info *info = to_m5mols(sd);
  611. u32 code;
  612. int ret;
  613. mutex_lock(&info->lock);
  614. code = info->ffmt[info->res_type].code;
  615. if (enable) {
  616. if (is_code(code, M5MOLS_RESTYPE_MONITOR))
  617. ret = m5mols_start_monitor(info);
  618. if (is_code(code, M5MOLS_RESTYPE_CAPTURE))
  619. ret = m5mols_start_capture(info);
  620. else
  621. ret = -EINVAL;
  622. } else {
  623. ret = m5mols_set_mode(info, REG_PARAMETER);
  624. }
  625. mutex_unlock(&info->lock);
  626. return ret;
  627. }
  628. static const struct v4l2_subdev_video_ops m5mols_video_ops = {
  629. .s_stream = m5mols_s_stream,
  630. };
  631. static int m5mols_sensor_power(struct m5mols_info *info, bool enable)
  632. {
  633. struct v4l2_subdev *sd = &info->sd;
  634. struct i2c_client *client = v4l2_get_subdevdata(sd);
  635. const struct m5mols_platform_data *pdata = info->pdata;
  636. int ret;
  637. if (info->power == enable)
  638. return 0;
  639. if (enable) {
  640. if (info->set_power) {
  641. ret = info->set_power(&client->dev, 1);
  642. if (ret)
  643. return ret;
  644. }
  645. ret = regulator_bulk_enable(ARRAY_SIZE(supplies), supplies);
  646. if (ret) {
  647. info->set_power(&client->dev, 0);
  648. return ret;
  649. }
  650. gpio_set_value(pdata->gpio_reset, !pdata->reset_polarity);
  651. info->power = 1;
  652. return ret;
  653. }
  654. ret = regulator_bulk_disable(ARRAY_SIZE(supplies), supplies);
  655. if (ret)
  656. return ret;
  657. if (info->set_power)
  658. info->set_power(&client->dev, 0);
  659. gpio_set_value(pdata->gpio_reset, pdata->reset_polarity);
  660. info->isp_ready = 0;
  661. info->power = 0;
  662. return ret;
  663. }
  664. /* m5mols_update_fw - optional firmware update routine */
  665. int __attribute__ ((weak)) m5mols_update_fw(struct v4l2_subdev *sd,
  666. int (*set_power)(struct m5mols_info *, bool))
  667. {
  668. return 0;
  669. }
  670. /**
  671. * m5mols_fw_start - M-5MOLS internal ARM controller initialization
  672. *
  673. * Execute the M-5MOLS internal ARM controller initialization sequence.
  674. * This function should be called after the supply voltage has been
  675. * applied and before any requests to the device are made.
  676. */
  677. static int m5mols_fw_start(struct v4l2_subdev *sd)
  678. {
  679. struct m5mols_info *info = to_m5mols(sd);
  680. int ret;
  681. atomic_set(&info->irq_done, 0);
  682. /* Wait until I2C slave is initialized in Flash Writer mode */
  683. ret = m5mols_busy_wait(sd, FLASH_CAM_START, REG_IN_FLASH_MODE,
  684. M5MOLS_I2C_RDY_WAIT_FL | 0xff, -1);
  685. if (!ret)
  686. ret = m5mols_write(sd, FLASH_CAM_START, REG_START_ARM_BOOT);
  687. if (!ret)
  688. ret = m5mols_wait_interrupt(sd, REG_INT_MODE, 2000);
  689. if (ret < 0)
  690. return ret;
  691. info->isp_ready = 1;
  692. ret = m5mols_get_version(sd);
  693. if (!ret)
  694. ret = m5mols_update_fw(sd, m5mols_sensor_power);
  695. if (ret)
  696. return ret;
  697. v4l2_dbg(1, m5mols_debug, sd, "Success ARM Booting\n");
  698. ret = m5mols_write(sd, PARM_INTERFACE, REG_INTERFACE_MIPI);
  699. if (!ret)
  700. ret = m5mols_enable_interrupt(sd,
  701. REG_INT_AF | REG_INT_CAPTURE);
  702. return ret;
  703. }
  704. /* Execute the lens soft-landing algorithm */
  705. static int m5mols_auto_focus_stop(struct m5mols_info *info)
  706. {
  707. int ret;
  708. ret = m5mols_write(&info->sd, AF_EXECUTE, REG_AF_STOP);
  709. if (!ret)
  710. ret = m5mols_write(&info->sd, AF_MODE, REG_AF_POWEROFF);
  711. if (!ret)
  712. ret = m5mols_busy_wait(&info->sd, SYSTEM_STATUS, REG_AF_IDLE,
  713. 0xff, -1);
  714. return ret;
  715. }
  716. /**
  717. * m5mols_s_power - Main sensor power control function
  718. *
  719. * To prevent breaking the lens when the sensor is powered off the Soft-Landing
  720. * algorithm is called where available. The Soft-Landing algorithm availability
  721. * dependends on the firmware provider.
  722. */
  723. static int m5mols_s_power(struct v4l2_subdev *sd, int on)
  724. {
  725. struct m5mols_info *info = to_m5mols(sd);
  726. int ret;
  727. mutex_lock(&info->lock);
  728. if (on) {
  729. ret = m5mols_sensor_power(info, true);
  730. if (!ret)
  731. ret = m5mols_fw_start(sd);
  732. } else {
  733. if (is_manufacturer(info, REG_SAMSUNG_TECHWIN)) {
  734. ret = m5mols_set_mode(info, REG_MONITOR);
  735. if (!ret)
  736. ret = m5mols_auto_focus_stop(info);
  737. if (ret < 0)
  738. v4l2_warn(sd, "Soft landing lens failed\n");
  739. }
  740. ret = m5mols_sensor_power(info, false);
  741. info->ctrl_sync = 0;
  742. }
  743. mutex_unlock(&info->lock);
  744. return ret;
  745. }
  746. static int m5mols_log_status(struct v4l2_subdev *sd)
  747. {
  748. struct m5mols_info *info = to_m5mols(sd);
  749. v4l2_ctrl_handler_log_status(&info->handle, sd->name);
  750. return 0;
  751. }
  752. static const struct v4l2_subdev_core_ops m5mols_core_ops = {
  753. .s_power = m5mols_s_power,
  754. .log_status = m5mols_log_status,
  755. };
  756. /*
  757. * V4L2 subdev internal operations
  758. */
  759. static int m5mols_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  760. {
  761. struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(fh, 0);
  762. *format = m5mols_default_ffmt[0];
  763. return 0;
  764. }
  765. static const struct v4l2_subdev_internal_ops m5mols_subdev_internal_ops = {
  766. .open = m5mols_open,
  767. };
  768. static const struct v4l2_subdev_ops m5mols_ops = {
  769. .core = &m5mols_core_ops,
  770. .pad = &m5mols_pad_ops,
  771. .video = &m5mols_video_ops,
  772. };
  773. static irqreturn_t m5mols_irq_handler(int irq, void *data)
  774. {
  775. struct m5mols_info *info = to_m5mols(data);
  776. atomic_set(&info->irq_done, 1);
  777. wake_up_interruptible(&info->irq_waitq);
  778. return IRQ_HANDLED;
  779. }
  780. static int m5mols_probe(struct i2c_client *client,
  781. const struct i2c_device_id *id)
  782. {
  783. const struct m5mols_platform_data *pdata = client->dev.platform_data;
  784. struct m5mols_info *info;
  785. struct v4l2_subdev *sd;
  786. int ret;
  787. if (pdata == NULL) {
  788. dev_err(&client->dev, "No platform data\n");
  789. return -EINVAL;
  790. }
  791. if (!gpio_is_valid(pdata->gpio_reset)) {
  792. dev_err(&client->dev, "No valid RESET GPIO specified\n");
  793. return -EINVAL;
  794. }
  795. if (!client->irq) {
  796. dev_err(&client->dev, "Interrupt not assigned\n");
  797. return -EINVAL;
  798. }
  799. info = kzalloc(sizeof(struct m5mols_info), GFP_KERNEL);
  800. if (!info)
  801. return -ENOMEM;
  802. info->pdata = pdata;
  803. info->set_power = pdata->set_power;
  804. ret = gpio_request(pdata->gpio_reset, "M5MOLS_NRST");
  805. if (ret) {
  806. dev_err(&client->dev, "Failed to request gpio: %d\n", ret);
  807. goto out_free;
  808. }
  809. gpio_direction_output(pdata->gpio_reset, pdata->reset_polarity);
  810. ret = regulator_bulk_get(&client->dev, ARRAY_SIZE(supplies), supplies);
  811. if (ret) {
  812. dev_err(&client->dev, "Failed to get regulators: %d\n", ret);
  813. goto out_gpio;
  814. }
  815. sd = &info->sd;
  816. v4l2_i2c_subdev_init(sd, client, &m5mols_ops);
  817. strlcpy(sd->name, MODULE_NAME, sizeof(sd->name));
  818. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  819. sd->internal_ops = &m5mols_subdev_internal_ops;
  820. info->pad.flags = MEDIA_PAD_FL_SOURCE;
  821. ret = media_entity_init(&sd->entity, 1, &info->pad, 0);
  822. if (ret < 0)
  823. goto out_reg;
  824. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
  825. init_waitqueue_head(&info->irq_waitq);
  826. mutex_init(&info->lock);
  827. ret = request_irq(client->irq, m5mols_irq_handler,
  828. IRQF_TRIGGER_RISING, MODULE_NAME, sd);
  829. if (ret) {
  830. dev_err(&client->dev, "Interrupt request failed: %d\n", ret);
  831. goto out_me;
  832. }
  833. info->res_type = M5MOLS_RESTYPE_MONITOR;
  834. info->ffmt[0] = m5mols_default_ffmt[0];
  835. info->ffmt[1] = m5mols_default_ffmt[1];
  836. ret = m5mols_sensor_power(info, true);
  837. if (ret)
  838. goto out_irq;
  839. ret = m5mols_fw_start(sd);
  840. if (!ret)
  841. ret = m5mols_init_controls(sd);
  842. ret = m5mols_sensor_power(info, false);
  843. if (!ret)
  844. return 0;
  845. out_irq:
  846. free_irq(client->irq, sd);
  847. out_me:
  848. media_entity_cleanup(&sd->entity);
  849. out_reg:
  850. regulator_bulk_free(ARRAY_SIZE(supplies), supplies);
  851. out_gpio:
  852. gpio_free(pdata->gpio_reset);
  853. out_free:
  854. kfree(info);
  855. return ret;
  856. }
  857. static int m5mols_remove(struct i2c_client *client)
  858. {
  859. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  860. struct m5mols_info *info = to_m5mols(sd);
  861. v4l2_device_unregister_subdev(sd);
  862. v4l2_ctrl_handler_free(sd->ctrl_handler);
  863. free_irq(client->irq, sd);
  864. regulator_bulk_free(ARRAY_SIZE(supplies), supplies);
  865. gpio_free(info->pdata->gpio_reset);
  866. media_entity_cleanup(&sd->entity);
  867. kfree(info);
  868. return 0;
  869. }
  870. static const struct i2c_device_id m5mols_id[] = {
  871. { MODULE_NAME, 0 },
  872. { },
  873. };
  874. MODULE_DEVICE_TABLE(i2c, m5mols_id);
  875. static struct i2c_driver m5mols_i2c_driver = {
  876. .driver = {
  877. .name = MODULE_NAME,
  878. },
  879. .probe = m5mols_probe,
  880. .remove = m5mols_remove,
  881. .id_table = m5mols_id,
  882. };
  883. module_i2c_driver(m5mols_i2c_driver);
  884. MODULE_AUTHOR("HeungJun Kim <riverful.kim@samsung.com>");
  885. MODULE_AUTHOR("Dongsoo Kim <dongsoo45.kim@samsung.com>");
  886. MODULE_DESCRIPTION("Fujitsu M-5MOLS 8M Pixel camera driver");
  887. MODULE_LICENSE("GPL");