m5mols_core.c 26 KB

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