m5mols_core.c 27 KB

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