m5mols_core.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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 = 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, 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. v4l2_info(sd, "Manufacturer\t[%s]\n",
  397. is_manufacturer(info, REG_SAMSUNG_ELECTRO) ?
  398. "Samsung Electro-Machanics" :
  399. is_manufacturer(info, REG_SAMSUNG_OPTICS) ?
  400. "Samsung Fiber-Optics" :
  401. is_manufacturer(info, REG_SAMSUNG_TECHWIN) ?
  402. "Samsung Techwin" : "None");
  403. v4l2_info(sd, "Customer/Project\t[0x%02x/0x%02x]\n",
  404. info->ver.customer, info->ver.project);
  405. if (!is_available_af(info))
  406. v4l2_info(sd, "No support Auto Focus on this firmware\n");
  407. return ret;
  408. }
  409. /**
  410. * __find_restype - Lookup M-5MOLS resolution type according to pixel code
  411. * @code: pixel code
  412. */
  413. static enum m5mols_restype __find_restype(u32 code)
  414. {
  415. enum m5mols_restype type = M5MOLS_RESTYPE_MONITOR;
  416. do {
  417. if (code == m5mols_default_ffmt[type].code)
  418. return type;
  419. } while (type++ != SIZE_DEFAULT_FFMT);
  420. return 0;
  421. }
  422. /**
  423. * __find_resolution - Lookup preset and type of M-5MOLS's resolution
  424. * @mf: pixel format to find/negotiate the resolution preset for
  425. * @type: M-5MOLS resolution type
  426. * @resolution: M-5MOLS resolution preset register value
  427. *
  428. * Find nearest resolution matching resolution preset and adjust mf
  429. * to supported values.
  430. */
  431. static int __find_resolution(struct v4l2_subdev *sd,
  432. struct v4l2_mbus_framefmt *mf,
  433. enum m5mols_restype *type,
  434. u32 *resolution)
  435. {
  436. const struct m5mols_resolution *fsize = &m5mols_reg_res[0];
  437. const struct m5mols_resolution *match = NULL;
  438. enum m5mols_restype stype = __find_restype(mf->code);
  439. int i = ARRAY_SIZE(m5mols_reg_res);
  440. unsigned int min_err = ~0;
  441. while (i--) {
  442. int err;
  443. if (stype == fsize->type) {
  444. err = abs(fsize->width - mf->width)
  445. + abs(fsize->height - mf->height);
  446. if (err < min_err) {
  447. min_err = err;
  448. match = fsize;
  449. }
  450. }
  451. fsize++;
  452. }
  453. if (match) {
  454. mf->width = match->width;
  455. mf->height = match->height;
  456. *resolution = match->reg;
  457. *type = stype;
  458. return 0;
  459. }
  460. return -EINVAL;
  461. }
  462. static struct v4l2_mbus_framefmt *__find_format(struct m5mols_info *info,
  463. struct v4l2_subdev_pad_config *cfg,
  464. enum v4l2_subdev_format_whence which,
  465. enum m5mols_restype type)
  466. {
  467. if (which == V4L2_SUBDEV_FORMAT_TRY)
  468. return cfg ? v4l2_subdev_get_try_format(&info->sd, cfg, 0) : NULL;
  469. return &info->ffmt[type];
  470. }
  471. static int m5mols_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
  472. struct v4l2_subdev_format *fmt)
  473. {
  474. struct m5mols_info *info = to_m5mols(sd);
  475. struct v4l2_mbus_framefmt *format;
  476. int ret = 0;
  477. mutex_lock(&info->lock);
  478. format = __find_format(info, cfg, fmt->which, info->res_type);
  479. if (format)
  480. fmt->format = *format;
  481. else
  482. ret = -EINVAL;
  483. mutex_unlock(&info->lock);
  484. return ret;
  485. }
  486. static int m5mols_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
  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, cfg, fmt->which, type);
  499. if (!sfmt)
  500. return 0;
  501. mutex_lock(&info->lock);
  502. format->code = m5mols_default_ffmt[type].code;
  503. format->colorspace = V4L2_COLORSPACE_JPEG;
  504. format->field = V4L2_FIELD_NONE;
  505. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  506. *sfmt = *format;
  507. info->resolution = resolution;
  508. info->res_type = type;
  509. }
  510. mutex_unlock(&info->lock);
  511. return ret;
  512. }
  513. static int m5mols_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
  514. struct v4l2_mbus_frame_desc *fd)
  515. {
  516. struct m5mols_info *info = to_m5mols(sd);
  517. if (pad != 0 || fd == NULL)
  518. return -EINVAL;
  519. mutex_lock(&info->lock);
  520. /*
  521. * .get_frame_desc is only used for compressed formats,
  522. * thus we always return the capture frame parameters here.
  523. */
  524. fd->entry[0].length = info->cap.buf_size;
  525. fd->entry[0].pixelcode = info->ffmt[M5MOLS_RESTYPE_CAPTURE].code;
  526. mutex_unlock(&info->lock);
  527. fd->entry[0].flags = V4L2_MBUS_FRAME_DESC_FL_LEN_MAX;
  528. fd->num_entries = 1;
  529. return 0;
  530. }
  531. static int m5mols_set_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
  532. struct v4l2_mbus_frame_desc *fd)
  533. {
  534. struct m5mols_info *info = to_m5mols(sd);
  535. struct v4l2_mbus_framefmt *mf = &info->ffmt[M5MOLS_RESTYPE_CAPTURE];
  536. if (pad != 0 || fd == NULL)
  537. return -EINVAL;
  538. fd->entry[0].flags = V4L2_MBUS_FRAME_DESC_FL_LEN_MAX;
  539. fd->num_entries = 1;
  540. fd->entry[0].length = clamp_t(u32, fd->entry[0].length,
  541. mf->width * mf->height,
  542. M5MOLS_MAIN_JPEG_SIZE_MAX);
  543. mutex_lock(&info->lock);
  544. info->cap.buf_size = fd->entry[0].length;
  545. mutex_unlock(&info->lock);
  546. return 0;
  547. }
  548. static int m5mols_enum_mbus_code(struct v4l2_subdev *sd,
  549. struct v4l2_subdev_pad_config *cfg,
  550. struct v4l2_subdev_mbus_code_enum *code)
  551. {
  552. if (!code || code->index >= SIZE_DEFAULT_FFMT)
  553. return -EINVAL;
  554. code->code = m5mols_default_ffmt[code->index].code;
  555. return 0;
  556. }
  557. static struct v4l2_subdev_pad_ops m5mols_pad_ops = {
  558. .enum_mbus_code = m5mols_enum_mbus_code,
  559. .get_fmt = m5mols_get_fmt,
  560. .set_fmt = m5mols_set_fmt,
  561. .get_frame_desc = m5mols_get_frame_desc,
  562. .set_frame_desc = m5mols_set_frame_desc,
  563. };
  564. /**
  565. * m5mols_restore_controls - Apply current control values to the registers
  566. *
  567. * m5mols_do_scenemode() handles all parameters for which there is yet no
  568. * individual control. It should be replaced at some point by setting each
  569. * control individually, in required register set up order.
  570. */
  571. int m5mols_restore_controls(struct m5mols_info *info)
  572. {
  573. int ret;
  574. if (info->ctrl_sync)
  575. return 0;
  576. ret = m5mols_do_scenemode(info, REG_SCENE_NORMAL);
  577. if (ret)
  578. return ret;
  579. ret = v4l2_ctrl_handler_setup(&info->handle);
  580. info->ctrl_sync = !ret;
  581. return ret;
  582. }
  583. /**
  584. * m5mols_start_monitor - Start the monitor mode
  585. *
  586. * Before applying the controls setup the resolution and frame rate
  587. * in PARAMETER mode, and then switch over to MONITOR mode.
  588. */
  589. static int m5mols_start_monitor(struct m5mols_info *info)
  590. {
  591. struct v4l2_subdev *sd = &info->sd;
  592. int ret;
  593. ret = m5mols_set_mode(info, REG_PARAMETER);
  594. if (!ret)
  595. ret = m5mols_write(sd, PARM_MON_SIZE, info->resolution);
  596. if (!ret)
  597. ret = m5mols_write(sd, PARM_MON_FPS, REG_FPS_30);
  598. if (!ret)
  599. ret = m5mols_set_mode(info, REG_MONITOR);
  600. if (!ret)
  601. ret = m5mols_restore_controls(info);
  602. return ret;
  603. }
  604. static int m5mols_s_stream(struct v4l2_subdev *sd, int enable)
  605. {
  606. struct m5mols_info *info = to_m5mols(sd);
  607. u32 code;
  608. int ret;
  609. mutex_lock(&info->lock);
  610. code = info->ffmt[info->res_type].code;
  611. if (enable) {
  612. if (is_code(code, M5MOLS_RESTYPE_MONITOR))
  613. ret = m5mols_start_monitor(info);
  614. else if (is_code(code, M5MOLS_RESTYPE_CAPTURE))
  615. ret = m5mols_start_capture(info);
  616. else
  617. ret = -EINVAL;
  618. } else {
  619. ret = m5mols_set_mode(info, REG_PARAMETER);
  620. }
  621. mutex_unlock(&info->lock);
  622. return ret;
  623. }
  624. static const struct v4l2_subdev_video_ops m5mols_video_ops = {
  625. .s_stream = m5mols_s_stream,
  626. };
  627. static int m5mols_sensor_power(struct m5mols_info *info, bool enable)
  628. {
  629. struct v4l2_subdev *sd = &info->sd;
  630. struct i2c_client *client = v4l2_get_subdevdata(sd);
  631. const struct m5mols_platform_data *pdata = info->pdata;
  632. int ret;
  633. if (info->power == enable)
  634. return 0;
  635. if (enable) {
  636. if (info->set_power) {
  637. ret = info->set_power(&client->dev, 1);
  638. if (ret)
  639. return ret;
  640. }
  641. ret = regulator_bulk_enable(ARRAY_SIZE(supplies), supplies);
  642. if (ret) {
  643. info->set_power(&client->dev, 0);
  644. return ret;
  645. }
  646. gpio_set_value(pdata->gpio_reset, !pdata->reset_polarity);
  647. info->power = 1;
  648. return ret;
  649. }
  650. ret = regulator_bulk_disable(ARRAY_SIZE(supplies), supplies);
  651. if (ret)
  652. return ret;
  653. if (info->set_power)
  654. info->set_power(&client->dev, 0);
  655. gpio_set_value(pdata->gpio_reset, pdata->reset_polarity);
  656. info->isp_ready = 0;
  657. info->power = 0;
  658. return ret;
  659. }
  660. /* m5mols_update_fw - optional firmware update routine */
  661. int __attribute__ ((weak)) m5mols_update_fw(struct v4l2_subdev *sd,
  662. int (*set_power)(struct m5mols_info *, bool))
  663. {
  664. return 0;
  665. }
  666. /**
  667. * m5mols_fw_start - M-5MOLS internal ARM controller initialization
  668. *
  669. * Execute the M-5MOLS internal ARM controller initialization sequence.
  670. * This function should be called after the supply voltage has been
  671. * applied and before any requests to the device are made.
  672. */
  673. static int m5mols_fw_start(struct v4l2_subdev *sd)
  674. {
  675. struct m5mols_info *info = to_m5mols(sd);
  676. int ret;
  677. atomic_set(&info->irq_done, 0);
  678. /* Wait until I2C slave is initialized in Flash Writer mode */
  679. ret = m5mols_busy_wait(sd, FLASH_CAM_START, REG_IN_FLASH_MODE,
  680. M5MOLS_I2C_RDY_WAIT_FL | 0xff, -1);
  681. if (!ret)
  682. ret = m5mols_write(sd, FLASH_CAM_START, REG_START_ARM_BOOT);
  683. if (!ret)
  684. ret = m5mols_wait_interrupt(sd, REG_INT_MODE, 2000);
  685. if (ret < 0)
  686. return ret;
  687. info->isp_ready = 1;
  688. ret = m5mols_get_version(sd);
  689. if (!ret)
  690. ret = m5mols_update_fw(sd, m5mols_sensor_power);
  691. if (ret)
  692. return ret;
  693. v4l2_dbg(1, m5mols_debug, sd, "Success ARM Booting\n");
  694. ret = m5mols_write(sd, PARM_INTERFACE, REG_INTERFACE_MIPI);
  695. if (!ret)
  696. ret = m5mols_enable_interrupt(sd,
  697. REG_INT_AF | REG_INT_CAPTURE);
  698. return ret;
  699. }
  700. /* Execute the lens soft-landing algorithm */
  701. static int m5mols_auto_focus_stop(struct m5mols_info *info)
  702. {
  703. int ret;
  704. ret = m5mols_write(&info->sd, AF_EXECUTE, REG_AF_STOP);
  705. if (!ret)
  706. ret = m5mols_write(&info->sd, AF_MODE, REG_AF_POWEROFF);
  707. if (!ret)
  708. ret = m5mols_busy_wait(&info->sd, SYSTEM_STATUS, REG_AF_IDLE,
  709. 0xff, -1);
  710. return ret;
  711. }
  712. /**
  713. * m5mols_s_power - Main sensor power control function
  714. *
  715. * To prevent breaking the lens when the sensor is powered off the Soft-Landing
  716. * algorithm is called where available. The Soft-Landing algorithm availability
  717. * dependends on the firmware provider.
  718. */
  719. static int m5mols_s_power(struct v4l2_subdev *sd, int on)
  720. {
  721. struct m5mols_info *info = to_m5mols(sd);
  722. int ret;
  723. mutex_lock(&info->lock);
  724. if (on) {
  725. ret = m5mols_sensor_power(info, true);
  726. if (!ret)
  727. ret = m5mols_fw_start(sd);
  728. } else {
  729. if (is_manufacturer(info, REG_SAMSUNG_TECHWIN)) {
  730. ret = m5mols_set_mode(info, REG_MONITOR);
  731. if (!ret)
  732. ret = m5mols_auto_focus_stop(info);
  733. if (ret < 0)
  734. v4l2_warn(sd, "Soft landing lens failed\n");
  735. }
  736. ret = m5mols_sensor_power(info, false);
  737. info->ctrl_sync = 0;
  738. }
  739. mutex_unlock(&info->lock);
  740. return ret;
  741. }
  742. static int m5mols_log_status(struct v4l2_subdev *sd)
  743. {
  744. struct m5mols_info *info = to_m5mols(sd);
  745. v4l2_ctrl_handler_log_status(&info->handle, sd->name);
  746. return 0;
  747. }
  748. static const struct v4l2_subdev_core_ops m5mols_core_ops = {
  749. .s_power = m5mols_s_power,
  750. .log_status = m5mols_log_status,
  751. };
  752. /*
  753. * V4L2 subdev internal operations
  754. */
  755. static int m5mols_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  756. {
  757. struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(sd, fh->pad, 0);
  758. *format = m5mols_default_ffmt[0];
  759. return 0;
  760. }
  761. static const struct v4l2_subdev_internal_ops m5mols_subdev_internal_ops = {
  762. .open = m5mols_open,
  763. };
  764. static const struct v4l2_subdev_ops m5mols_ops = {
  765. .core = &m5mols_core_ops,
  766. .pad = &m5mols_pad_ops,
  767. .video = &m5mols_video_ops,
  768. };
  769. static irqreturn_t m5mols_irq_handler(int irq, void *data)
  770. {
  771. struct m5mols_info *info = to_m5mols(data);
  772. atomic_set(&info->irq_done, 1);
  773. wake_up_interruptible(&info->irq_waitq);
  774. return IRQ_HANDLED;
  775. }
  776. static int m5mols_probe(struct i2c_client *client,
  777. const struct i2c_device_id *id)
  778. {
  779. const struct m5mols_platform_data *pdata = client->dev.platform_data;
  780. unsigned long gpio_flags;
  781. struct m5mols_info *info;
  782. struct v4l2_subdev *sd;
  783. int ret;
  784. if (pdata == NULL) {
  785. dev_err(&client->dev, "No platform data\n");
  786. return -EINVAL;
  787. }
  788. if (!gpio_is_valid(pdata->gpio_reset)) {
  789. dev_err(&client->dev, "No valid RESET GPIO specified\n");
  790. return -EINVAL;
  791. }
  792. if (!client->irq) {
  793. dev_err(&client->dev, "Interrupt not assigned\n");
  794. return -EINVAL;
  795. }
  796. info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
  797. if (!info)
  798. return -ENOMEM;
  799. info->pdata = pdata;
  800. info->set_power = pdata->set_power;
  801. gpio_flags = pdata->reset_polarity
  802. ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  803. ret = devm_gpio_request_one(&client->dev, pdata->gpio_reset, gpio_flags,
  804. "M5MOLS_NRST");
  805. if (ret) {
  806. dev_err(&client->dev, "Failed to request gpio: %d\n", ret);
  807. return ret;
  808. }
  809. ret = devm_regulator_bulk_get(&client->dev, ARRAY_SIZE(supplies),
  810. supplies);
  811. if (ret) {
  812. dev_err(&client->dev, "Failed to get regulators: %d\n", ret);
  813. return ret;
  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. return ret;
  824. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
  825. init_waitqueue_head(&info->irq_waitq);
  826. mutex_init(&info->lock);
  827. ret = devm_request_irq(&client->dev, 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 error;
  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 error;
  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. error:
  846. media_entity_cleanup(&sd->entity);
  847. return ret;
  848. }
  849. static int m5mols_remove(struct i2c_client *client)
  850. {
  851. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  852. v4l2_device_unregister_subdev(sd);
  853. v4l2_ctrl_handler_free(sd->ctrl_handler);
  854. media_entity_cleanup(&sd->entity);
  855. return 0;
  856. }
  857. static const struct i2c_device_id m5mols_id[] = {
  858. { MODULE_NAME, 0 },
  859. { },
  860. };
  861. MODULE_DEVICE_TABLE(i2c, m5mols_id);
  862. static struct i2c_driver m5mols_i2c_driver = {
  863. .driver = {
  864. .name = MODULE_NAME,
  865. },
  866. .probe = m5mols_probe,
  867. .remove = m5mols_remove,
  868. .id_table = m5mols_id,
  869. };
  870. module_i2c_driver(m5mols_i2c_driver);
  871. MODULE_AUTHOR("HeungJun Kim <riverful.kim@samsung.com>");
  872. MODULE_AUTHOR("Dongsoo Kim <dongsoo45.kim@samsung.com>");
  873. MODULE_DESCRIPTION("Fujitsu M-5MOLS 8M Pixel camera driver");
  874. MODULE_LICENSE("GPL");