m5mols_core.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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/i2c/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 = MEDIA_BUS_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 = MEDIA_BUS_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(*((__be16 *)data));
  120. else
  121. return be32_to_cpu(*((__be32 *)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, 300);
  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. /* minimum stabilization time */
  238. usleep_range(200, 300);
  239. ret = i2c_transfer(client->adapter, msg, 1);
  240. if (ret == 1)
  241. return 0;
  242. if (info->isp_ready)
  243. v4l2_err(sd, "write failed: cat:%02x cmd:%02x ret:%d\n",
  244. category, cmd, ret);
  245. return ret < 0 ? ret : -EIO;
  246. }
  247. /**
  248. * m5mols_busy_wait - Busy waiting with I2C register polling
  249. * @reg: the I2C_REG() address of an 8-bit status register to check
  250. * @value: expected status register value
  251. * @mask: bit mask for the read status register value
  252. * @timeout: timeout in miliseconds, or -1 for default timeout
  253. *
  254. * The @reg register value is ORed with @mask before comparing with @value.
  255. *
  256. * Return: 0 if the requested condition became true within less than
  257. * @timeout ms, or else negative errno.
  258. */
  259. int m5mols_busy_wait(struct v4l2_subdev *sd, u32 reg, u32 value, u32 mask,
  260. int timeout)
  261. {
  262. int ms = timeout < 0 ? M5MOLS_BUSY_WAIT_DEF_TIMEOUT : timeout;
  263. unsigned long end = jiffies + msecs_to_jiffies(ms);
  264. u8 status;
  265. do {
  266. int ret = m5mols_read_u8(sd, reg, &status);
  267. if (ret < 0 && !(mask & M5MOLS_I2C_RDY_WAIT_FL))
  268. return ret;
  269. if (!ret && (status & mask & 0xff) == (value & 0xff))
  270. return 0;
  271. usleep_range(100, 250);
  272. } while (ms > 0 && time_is_after_jiffies(end));
  273. return -EBUSY;
  274. }
  275. /**
  276. * m5mols_enable_interrupt - Clear interrupt pending bits and unmask interrupts
  277. *
  278. * Before writing desired interrupt value the INT_FACTOR register should
  279. * be read to clear pending interrupts.
  280. */
  281. int m5mols_enable_interrupt(struct v4l2_subdev *sd, u8 reg)
  282. {
  283. struct m5mols_info *info = to_m5mols(sd);
  284. u8 mask = is_available_af(info) ? REG_INT_AF : 0;
  285. u8 dummy;
  286. int ret;
  287. ret = m5mols_read_u8(sd, SYSTEM_INT_FACTOR, &dummy);
  288. if (!ret)
  289. ret = m5mols_write(sd, SYSTEM_INT_ENABLE, reg & ~mask);
  290. return ret;
  291. }
  292. int m5mols_wait_interrupt(struct v4l2_subdev *sd, u8 irq_mask, u32 timeout)
  293. {
  294. struct m5mols_info *info = to_m5mols(sd);
  295. int ret = wait_event_interruptible_timeout(info->irq_waitq,
  296. atomic_add_unless(&info->irq_done, -1, 0),
  297. msecs_to_jiffies(timeout));
  298. if (ret <= 0)
  299. return ret ? ret : -ETIMEDOUT;
  300. return m5mols_busy_wait(sd, SYSTEM_INT_FACTOR, irq_mask,
  301. M5MOLS_I2C_RDY_WAIT_FL | irq_mask, -1);
  302. }
  303. /**
  304. * m5mols_reg_mode - Write the mode and check busy status
  305. *
  306. * It always accompanies a little delay changing the M-5MOLS mode, so it is
  307. * needed checking current busy status to guarantee right mode.
  308. */
  309. static int m5mols_reg_mode(struct v4l2_subdev *sd, u8 mode)
  310. {
  311. int ret = m5mols_write(sd, SYSTEM_SYSMODE, mode);
  312. if (ret < 0)
  313. return ret;
  314. return m5mols_busy_wait(sd, SYSTEM_SYSMODE, mode, 0xff,
  315. M5MOLS_MODE_CHANGE_TIMEOUT);
  316. }
  317. /**
  318. * m5mols_set_mode - set the M-5MOLS controller mode
  319. * @mode: the required operation mode
  320. *
  321. * The commands of M-5MOLS are grouped into specific modes. Each functionality
  322. * can be guaranteed only when the sensor is operating in mode which a command
  323. * belongs to.
  324. */
  325. int m5mols_set_mode(struct m5mols_info *info, u8 mode)
  326. {
  327. struct v4l2_subdev *sd = &info->sd;
  328. int ret = -EINVAL;
  329. u8 reg;
  330. if (mode < REG_PARAMETER || mode > REG_CAPTURE)
  331. return ret;
  332. ret = m5mols_read_u8(sd, SYSTEM_SYSMODE, &reg);
  333. if (ret || reg == mode)
  334. return ret;
  335. switch (reg) {
  336. case REG_PARAMETER:
  337. ret = m5mols_reg_mode(sd, REG_MONITOR);
  338. if (mode == REG_MONITOR)
  339. break;
  340. if (!ret)
  341. ret = m5mols_reg_mode(sd, REG_CAPTURE);
  342. break;
  343. case REG_MONITOR:
  344. if (mode == REG_PARAMETER) {
  345. ret = m5mols_reg_mode(sd, REG_PARAMETER);
  346. break;
  347. }
  348. ret = m5mols_reg_mode(sd, REG_CAPTURE);
  349. break;
  350. case REG_CAPTURE:
  351. ret = m5mols_reg_mode(sd, REG_MONITOR);
  352. if (mode == REG_MONITOR)
  353. break;
  354. if (!ret)
  355. ret = m5mols_reg_mode(sd, REG_PARAMETER);
  356. break;
  357. default:
  358. v4l2_warn(sd, "Wrong mode: %d\n", mode);
  359. }
  360. if (!ret)
  361. info->mode = mode;
  362. return ret;
  363. }
  364. /**
  365. * m5mols_get_version - retrieve full revisions information of M-5MOLS
  366. *
  367. * The version information includes revisions of hardware and firmware,
  368. * AutoFocus alghorithm version and the version string.
  369. */
  370. static int m5mols_get_version(struct v4l2_subdev *sd)
  371. {
  372. struct m5mols_info *info = to_m5mols(sd);
  373. struct m5mols_version *ver = &info->ver;
  374. u8 *str = ver->str;
  375. int i;
  376. int ret;
  377. ret = m5mols_read_u8(sd, SYSTEM_VER_CUSTOMER, &ver->customer);
  378. if (!ret)
  379. ret = m5mols_read_u8(sd, SYSTEM_VER_PROJECT, &ver->project);
  380. if (!ret)
  381. ret = m5mols_read_u16(sd, SYSTEM_VER_FIRMWARE, &ver->fw);
  382. if (!ret)
  383. ret = m5mols_read_u16(sd, SYSTEM_VER_HARDWARE, &ver->hw);
  384. if (!ret)
  385. ret = m5mols_read_u16(sd, SYSTEM_VER_PARAMETER, &ver->param);
  386. if (!ret)
  387. ret = m5mols_read_u16(sd, SYSTEM_VER_AWB, &ver->awb);
  388. if (!ret)
  389. ret = m5mols_read_u8(sd, AF_VERSION, &ver->af);
  390. if (ret)
  391. return ret;
  392. for (i = 0; i < VERSION_STRING_SIZE; i++) {
  393. ret = m5mols_read_u8(sd, SYSTEM_VER_STRING, &str[i]);
  394. if (ret)
  395. return ret;
  396. }
  397. v4l2_info(sd, "Manufacturer\t[%s]\n",
  398. is_manufacturer(info, REG_SAMSUNG_ELECTRO) ?
  399. "Samsung Electro-Machanics" :
  400. is_manufacturer(info, REG_SAMSUNG_OPTICS) ?
  401. "Samsung Fiber-Optics" :
  402. is_manufacturer(info, REG_SAMSUNG_TECHWIN) ?
  403. "Samsung Techwin" : "None");
  404. v4l2_info(sd, "Customer/Project\t[0x%02x/0x%02x]\n",
  405. info->ver.customer, info->ver.project);
  406. if (!is_available_af(info))
  407. v4l2_info(sd, "No support Auto Focus on this firmware\n");
  408. return ret;
  409. }
  410. /**
  411. * __find_restype - Lookup M-5MOLS resolution type according to pixel code
  412. * @code: pixel code
  413. */
  414. static enum m5mols_restype __find_restype(u32 code)
  415. {
  416. enum m5mols_restype type = M5MOLS_RESTYPE_MONITOR;
  417. do {
  418. if (code == m5mols_default_ffmt[type].code)
  419. return type;
  420. } while (type++ != SIZE_DEFAULT_FFMT);
  421. return 0;
  422. }
  423. /**
  424. * __find_resolution - Lookup preset and type of M-5MOLS's resolution
  425. * @mf: pixel format to find/negotiate the resolution preset for
  426. * @type: M-5MOLS resolution type
  427. * @resolution: M-5MOLS resolution preset register value
  428. *
  429. * Find nearest resolution matching resolution preset and adjust mf
  430. * to supported values.
  431. */
  432. static int __find_resolution(struct v4l2_subdev *sd,
  433. struct v4l2_mbus_framefmt *mf,
  434. enum m5mols_restype *type,
  435. u32 *resolution)
  436. {
  437. const struct m5mols_resolution *fsize = &m5mols_reg_res[0];
  438. const struct m5mols_resolution *match = NULL;
  439. enum m5mols_restype stype = __find_restype(mf->code);
  440. int i = ARRAY_SIZE(m5mols_reg_res);
  441. unsigned int min_err = ~0;
  442. while (i--) {
  443. int err;
  444. if (stype == fsize->type) {
  445. err = abs(fsize->width - mf->width)
  446. + abs(fsize->height - mf->height);
  447. if (err < min_err) {
  448. min_err = err;
  449. match = fsize;
  450. }
  451. }
  452. fsize++;
  453. }
  454. if (match) {
  455. mf->width = match->width;
  456. mf->height = match->height;
  457. *resolution = match->reg;
  458. *type = stype;
  459. return 0;
  460. }
  461. return -EINVAL;
  462. }
  463. static struct v4l2_mbus_framefmt *__find_format(struct m5mols_info *info,
  464. struct v4l2_subdev_pad_config *cfg,
  465. enum v4l2_subdev_format_whence which,
  466. enum m5mols_restype type)
  467. {
  468. if (which == V4L2_SUBDEV_FORMAT_TRY)
  469. return cfg ? v4l2_subdev_get_try_format(&info->sd, cfg, 0) : NULL;
  470. return &info->ffmt[type];
  471. }
  472. static int m5mols_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
  473. struct v4l2_subdev_format *fmt)
  474. {
  475. struct m5mols_info *info = to_m5mols(sd);
  476. struct v4l2_mbus_framefmt *format;
  477. int ret = 0;
  478. mutex_lock(&info->lock);
  479. format = __find_format(info, cfg, fmt->which, info->res_type);
  480. if (format)
  481. fmt->format = *format;
  482. else
  483. ret = -EINVAL;
  484. mutex_unlock(&info->lock);
  485. return ret;
  486. }
  487. static int m5mols_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
  488. struct v4l2_subdev_format *fmt)
  489. {
  490. struct m5mols_info *info = to_m5mols(sd);
  491. struct v4l2_mbus_framefmt *format = &fmt->format;
  492. struct v4l2_mbus_framefmt *sfmt;
  493. enum m5mols_restype type;
  494. u32 resolution = 0;
  495. int ret;
  496. ret = __find_resolution(sd, format, &type, &resolution);
  497. if (ret < 0)
  498. return ret;
  499. sfmt = __find_format(info, cfg, fmt->which, type);
  500. if (!sfmt)
  501. return 0;
  502. mutex_lock(&info->lock);
  503. format->code = m5mols_default_ffmt[type].code;
  504. format->colorspace = V4L2_COLORSPACE_JPEG;
  505. format->field = V4L2_FIELD_NONE;
  506. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  507. *sfmt = *format;
  508. info->resolution = resolution;
  509. info->res_type = type;
  510. }
  511. mutex_unlock(&info->lock);
  512. return ret;
  513. }
  514. static int m5mols_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
  515. struct v4l2_mbus_frame_desc *fd)
  516. {
  517. struct m5mols_info *info = to_m5mols(sd);
  518. if (pad != 0 || fd == NULL)
  519. return -EINVAL;
  520. mutex_lock(&info->lock);
  521. /*
  522. * .get_frame_desc is only used for compressed formats,
  523. * thus we always return the capture frame parameters here.
  524. */
  525. fd->entry[0].length = info->cap.buf_size;
  526. fd->entry[0].pixelcode = info->ffmt[M5MOLS_RESTYPE_CAPTURE].code;
  527. mutex_unlock(&info->lock);
  528. fd->entry[0].flags = V4L2_MBUS_FRAME_DESC_FL_LEN_MAX;
  529. fd->num_entries = 1;
  530. return 0;
  531. }
  532. static int m5mols_set_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
  533. struct v4l2_mbus_frame_desc *fd)
  534. {
  535. struct m5mols_info *info = to_m5mols(sd);
  536. struct v4l2_mbus_framefmt *mf = &info->ffmt[M5MOLS_RESTYPE_CAPTURE];
  537. if (pad != 0 || fd == NULL)
  538. return -EINVAL;
  539. fd->entry[0].flags = V4L2_MBUS_FRAME_DESC_FL_LEN_MAX;
  540. fd->num_entries = 1;
  541. fd->entry[0].length = clamp_t(u32, fd->entry[0].length,
  542. mf->width * mf->height,
  543. M5MOLS_MAIN_JPEG_SIZE_MAX);
  544. mutex_lock(&info->lock);
  545. info->cap.buf_size = fd->entry[0].length;
  546. mutex_unlock(&info->lock);
  547. return 0;
  548. }
  549. static int m5mols_enum_mbus_code(struct v4l2_subdev *sd,
  550. struct v4l2_subdev_pad_config *cfg,
  551. struct v4l2_subdev_mbus_code_enum *code)
  552. {
  553. if (!code || code->index >= SIZE_DEFAULT_FFMT)
  554. return -EINVAL;
  555. code->code = m5mols_default_ffmt[code->index].code;
  556. return 0;
  557. }
  558. static const struct v4l2_subdev_pad_ops m5mols_pad_ops = {
  559. .enum_mbus_code = m5mols_enum_mbus_code,
  560. .get_fmt = m5mols_get_fmt,
  561. .set_fmt = m5mols_set_fmt,
  562. .get_frame_desc = m5mols_get_frame_desc,
  563. .set_frame_desc = m5mols_set_frame_desc,
  564. };
  565. /**
  566. * m5mols_restore_controls - Apply current control values to the registers
  567. *
  568. * m5mols_do_scenemode() handles all parameters for which there is yet no
  569. * individual control. It should be replaced at some point by setting each
  570. * control individually, in required register set up order.
  571. */
  572. int m5mols_restore_controls(struct m5mols_info *info)
  573. {
  574. int ret;
  575. if (info->ctrl_sync)
  576. return 0;
  577. ret = m5mols_do_scenemode(info, REG_SCENE_NORMAL);
  578. if (ret)
  579. return ret;
  580. ret = v4l2_ctrl_handler_setup(&info->handle);
  581. info->ctrl_sync = !ret;
  582. return ret;
  583. }
  584. /**
  585. * m5mols_start_monitor - Start the monitor mode
  586. *
  587. * Before applying the controls setup the resolution and frame rate
  588. * in PARAMETER mode, and then switch over to MONITOR mode.
  589. */
  590. static int m5mols_start_monitor(struct m5mols_info *info)
  591. {
  592. struct v4l2_subdev *sd = &info->sd;
  593. int ret;
  594. ret = m5mols_set_mode(info, REG_PARAMETER);
  595. if (!ret)
  596. ret = m5mols_write(sd, PARM_MON_SIZE, info->resolution);
  597. if (!ret)
  598. ret = m5mols_write(sd, PARM_MON_FPS, REG_FPS_30);
  599. if (!ret)
  600. ret = m5mols_set_mode(info, REG_MONITOR);
  601. if (!ret)
  602. ret = m5mols_restore_controls(info);
  603. return ret;
  604. }
  605. static int m5mols_s_stream(struct v4l2_subdev *sd, int enable)
  606. {
  607. struct m5mols_info *info = to_m5mols(sd);
  608. u32 code;
  609. int ret;
  610. mutex_lock(&info->lock);
  611. code = info->ffmt[info->res_type].code;
  612. if (enable) {
  613. if (is_code(code, M5MOLS_RESTYPE_MONITOR))
  614. ret = m5mols_start_monitor(info);
  615. else if (is_code(code, M5MOLS_RESTYPE_CAPTURE))
  616. ret = m5mols_start_capture(info);
  617. else
  618. ret = -EINVAL;
  619. } else {
  620. ret = m5mols_set_mode(info, REG_PARAMETER);
  621. }
  622. mutex_unlock(&info->lock);
  623. return ret;
  624. }
  625. static const struct v4l2_subdev_video_ops m5mols_video_ops = {
  626. .s_stream = m5mols_s_stream,
  627. };
  628. static int m5mols_sensor_power(struct m5mols_info *info, bool enable)
  629. {
  630. struct v4l2_subdev *sd = &info->sd;
  631. struct i2c_client *client = v4l2_get_subdevdata(sd);
  632. const struct m5mols_platform_data *pdata = info->pdata;
  633. int ret;
  634. if (info->power == enable)
  635. return 0;
  636. if (enable) {
  637. if (info->set_power) {
  638. ret = info->set_power(&client->dev, 1);
  639. if (ret)
  640. return ret;
  641. }
  642. ret = regulator_bulk_enable(ARRAY_SIZE(supplies), supplies);
  643. if (ret) {
  644. info->set_power(&client->dev, 0);
  645. return ret;
  646. }
  647. gpio_set_value(pdata->gpio_reset, !pdata->reset_polarity);
  648. info->power = 1;
  649. return ret;
  650. }
  651. ret = regulator_bulk_disable(ARRAY_SIZE(supplies), supplies);
  652. if (ret)
  653. return ret;
  654. if (info->set_power)
  655. info->set_power(&client->dev, 0);
  656. gpio_set_value(pdata->gpio_reset, pdata->reset_polarity);
  657. info->isp_ready = 0;
  658. info->power = 0;
  659. return ret;
  660. }
  661. /* m5mols_update_fw - optional firmware update routine */
  662. int __attribute__ ((weak)) m5mols_update_fw(struct v4l2_subdev *sd,
  663. int (*set_power)(struct m5mols_info *, bool))
  664. {
  665. return 0;
  666. }
  667. /**
  668. * m5mols_fw_start - M-5MOLS internal ARM controller initialization
  669. *
  670. * Execute the M-5MOLS internal ARM controller initialization sequence.
  671. * This function should be called after the supply voltage has been
  672. * applied and before any requests to the device are made.
  673. */
  674. static int m5mols_fw_start(struct v4l2_subdev *sd)
  675. {
  676. struct m5mols_info *info = to_m5mols(sd);
  677. int ret;
  678. atomic_set(&info->irq_done, 0);
  679. /* Wait until I2C slave is initialized in Flash Writer mode */
  680. ret = m5mols_busy_wait(sd, FLASH_CAM_START, REG_IN_FLASH_MODE,
  681. M5MOLS_I2C_RDY_WAIT_FL | 0xff, -1);
  682. if (!ret)
  683. ret = m5mols_write(sd, FLASH_CAM_START, REG_START_ARM_BOOT);
  684. if (!ret)
  685. ret = m5mols_wait_interrupt(sd, REG_INT_MODE, 2000);
  686. if (ret < 0)
  687. return ret;
  688. info->isp_ready = 1;
  689. ret = m5mols_get_version(sd);
  690. if (!ret)
  691. ret = m5mols_update_fw(sd, m5mols_sensor_power);
  692. if (ret)
  693. return ret;
  694. v4l2_dbg(1, m5mols_debug, sd, "Success ARM Booting\n");
  695. ret = m5mols_write(sd, PARM_INTERFACE, REG_INTERFACE_MIPI);
  696. if (!ret)
  697. ret = m5mols_enable_interrupt(sd,
  698. REG_INT_AF | REG_INT_CAPTURE);
  699. return ret;
  700. }
  701. /* Execute the lens soft-landing algorithm */
  702. static int m5mols_auto_focus_stop(struct m5mols_info *info)
  703. {
  704. int ret;
  705. ret = m5mols_write(&info->sd, AF_EXECUTE, REG_AF_STOP);
  706. if (!ret)
  707. ret = m5mols_write(&info->sd, AF_MODE, REG_AF_POWEROFF);
  708. if (!ret)
  709. ret = m5mols_busy_wait(&info->sd, SYSTEM_STATUS, REG_AF_IDLE,
  710. 0xff, -1);
  711. return ret;
  712. }
  713. /**
  714. * m5mols_s_power - Main sensor power control function
  715. *
  716. * To prevent breaking the lens when the sensor is powered off the Soft-Landing
  717. * algorithm is called where available. The Soft-Landing algorithm availability
  718. * dependends on the firmware provider.
  719. */
  720. static int m5mols_s_power(struct v4l2_subdev *sd, int on)
  721. {
  722. struct m5mols_info *info = to_m5mols(sd);
  723. int ret;
  724. mutex_lock(&info->lock);
  725. if (on) {
  726. ret = m5mols_sensor_power(info, true);
  727. if (!ret)
  728. ret = m5mols_fw_start(sd);
  729. } else {
  730. if (is_manufacturer(info, REG_SAMSUNG_TECHWIN)) {
  731. ret = m5mols_set_mode(info, REG_MONITOR);
  732. if (!ret)
  733. ret = m5mols_auto_focus_stop(info);
  734. if (ret < 0)
  735. v4l2_warn(sd, "Soft landing lens failed\n");
  736. }
  737. ret = m5mols_sensor_power(info, false);
  738. info->ctrl_sync = 0;
  739. }
  740. mutex_unlock(&info->lock);
  741. return ret;
  742. }
  743. static int m5mols_log_status(struct v4l2_subdev *sd)
  744. {
  745. struct m5mols_info *info = to_m5mols(sd);
  746. v4l2_ctrl_handler_log_status(&info->handle, sd->name);
  747. return 0;
  748. }
  749. static const struct v4l2_subdev_core_ops m5mols_core_ops = {
  750. .s_power = m5mols_s_power,
  751. .log_status = m5mols_log_status,
  752. };
  753. /*
  754. * V4L2 subdev internal operations
  755. */
  756. static int m5mols_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  757. {
  758. struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(sd, fh->pad, 0);
  759. *format = m5mols_default_ffmt[0];
  760. return 0;
  761. }
  762. static const struct v4l2_subdev_internal_ops m5mols_subdev_internal_ops = {
  763. .open = m5mols_open,
  764. };
  765. static const struct v4l2_subdev_ops m5mols_ops = {
  766. .core = &m5mols_core_ops,
  767. .pad = &m5mols_pad_ops,
  768. .video = &m5mols_video_ops,
  769. };
  770. static irqreturn_t m5mols_irq_handler(int irq, void *data)
  771. {
  772. struct m5mols_info *info = to_m5mols(data);
  773. atomic_set(&info->irq_done, 1);
  774. wake_up_interruptible(&info->irq_waitq);
  775. return IRQ_HANDLED;
  776. }
  777. static int m5mols_probe(struct i2c_client *client,
  778. const struct i2c_device_id *id)
  779. {
  780. const struct m5mols_platform_data *pdata = client->dev.platform_data;
  781. unsigned long gpio_flags;
  782. struct m5mols_info *info;
  783. struct v4l2_subdev *sd;
  784. int ret;
  785. if (pdata == NULL) {
  786. dev_err(&client->dev, "No platform data\n");
  787. return -EINVAL;
  788. }
  789. if (!gpio_is_valid(pdata->gpio_reset)) {
  790. dev_err(&client->dev, "No valid RESET GPIO specified\n");
  791. return -EINVAL;
  792. }
  793. if (!client->irq) {
  794. dev_err(&client->dev, "Interrupt not assigned\n");
  795. return -EINVAL;
  796. }
  797. info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
  798. if (!info)
  799. return -ENOMEM;
  800. info->pdata = pdata;
  801. info->set_power = pdata->set_power;
  802. gpio_flags = pdata->reset_polarity
  803. ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  804. ret = devm_gpio_request_one(&client->dev, pdata->gpio_reset, gpio_flags,
  805. "M5MOLS_NRST");
  806. if (ret) {
  807. dev_err(&client->dev, "Failed to request gpio: %d\n", ret);
  808. return ret;
  809. }
  810. ret = devm_regulator_bulk_get(&client->dev, ARRAY_SIZE(supplies),
  811. supplies);
  812. if (ret) {
  813. dev_err(&client->dev, "Failed to get regulators: %d\n", ret);
  814. return ret;
  815. }
  816. sd = &info->sd;
  817. v4l2_i2c_subdev_init(sd, client, &m5mols_ops);
  818. strlcpy(sd->name, MODULE_NAME, sizeof(sd->name));
  819. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  820. sd->internal_ops = &m5mols_subdev_internal_ops;
  821. info->pad.flags = MEDIA_PAD_FL_SOURCE;
  822. ret = media_entity_pads_init(&sd->entity, 1, &info->pad);
  823. if (ret < 0)
  824. return ret;
  825. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  826. init_waitqueue_head(&info->irq_waitq);
  827. mutex_init(&info->lock);
  828. ret = devm_request_irq(&client->dev, client->irq, m5mols_irq_handler,
  829. IRQF_TRIGGER_RISING, MODULE_NAME, sd);
  830. if (ret) {
  831. dev_err(&client->dev, "Interrupt request failed: %d\n", ret);
  832. goto error;
  833. }
  834. info->res_type = M5MOLS_RESTYPE_MONITOR;
  835. info->ffmt[0] = m5mols_default_ffmt[0];
  836. info->ffmt[1] = m5mols_default_ffmt[1];
  837. ret = m5mols_sensor_power(info, true);
  838. if (ret)
  839. goto error;
  840. ret = m5mols_fw_start(sd);
  841. if (!ret)
  842. ret = m5mols_init_controls(sd);
  843. ret = m5mols_sensor_power(info, false);
  844. if (!ret)
  845. return 0;
  846. error:
  847. media_entity_cleanup(&sd->entity);
  848. return ret;
  849. }
  850. static int m5mols_remove(struct i2c_client *client)
  851. {
  852. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  853. v4l2_device_unregister_subdev(sd);
  854. v4l2_ctrl_handler_free(sd->ctrl_handler);
  855. media_entity_cleanup(&sd->entity);
  856. return 0;
  857. }
  858. static const struct i2c_device_id m5mols_id[] = {
  859. { MODULE_NAME, 0 },
  860. { },
  861. };
  862. MODULE_DEVICE_TABLE(i2c, m5mols_id);
  863. static struct i2c_driver m5mols_i2c_driver = {
  864. .driver = {
  865. .name = MODULE_NAME,
  866. },
  867. .probe = m5mols_probe,
  868. .remove = m5mols_remove,
  869. .id_table = m5mols_id,
  870. };
  871. module_i2c_driver(m5mols_i2c_driver);
  872. MODULE_AUTHOR("HeungJun Kim <riverful.kim@samsung.com>");
  873. MODULE_AUTHOR("Dongsoo Kim <dongsoo45.kim@samsung.com>");
  874. MODULE_DESCRIPTION("Fujitsu M-5MOLS 8M Pixel camera driver");
  875. MODULE_LICENSE("GPL");