tcm825x.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /*
  2. * drivers/media/video/tcm825x.c
  3. *
  4. * TCM825X camera sensor driver.
  5. *
  6. * Copyright (C) 2007 Nokia Corporation.
  7. *
  8. * Contact: Sakari Ailus <sakari.ailus@nokia.com>
  9. *
  10. * Based on code from David Cohen <david.cohen@indt.org.br>
  11. *
  12. * This driver was based on ov9640 sensor driver from MontaVista
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * version 2 as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful, but
  19. * WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  26. * 02110-1301 USA
  27. */
  28. #include <linux/i2c.h>
  29. #include <linux/module.h>
  30. #include <media/v4l2-int-device.h>
  31. #include "tcm825x.h"
  32. /*
  33. * The sensor has two fps modes: the lower one just gives half the fps
  34. * at the same xclk than the high one.
  35. */
  36. #define MAX_FPS 30
  37. #define MIN_FPS 8
  38. #define MAX_HALF_FPS (MAX_FPS / 2)
  39. #define HIGH_FPS_MODE_LOWER_LIMIT 14
  40. #define DEFAULT_FPS MAX_HALF_FPS
  41. struct tcm825x_sensor {
  42. const struct tcm825x_platform_data *platform_data;
  43. struct v4l2_int_device *v4l2_int_device;
  44. struct i2c_client *i2c_client;
  45. struct v4l2_pix_format pix;
  46. struct v4l2_fract timeperframe;
  47. };
  48. /* list of image formats supported by TCM825X sensor */
  49. static const struct v4l2_fmtdesc tcm825x_formats[] = {
  50. {
  51. .description = "YUYV (YUV 4:2:2), packed",
  52. .pixelformat = V4L2_PIX_FMT_UYVY,
  53. }, {
  54. /* Note: V4L2 defines RGB565 as:
  55. *
  56. * Byte 0 Byte 1
  57. * g2 g1 g0 r4 r3 r2 r1 r0 b4 b3 b2 b1 b0 g5 g4 g3
  58. *
  59. * We interpret RGB565 as:
  60. *
  61. * Byte 0 Byte 1
  62. * g2 g1 g0 b4 b3 b2 b1 b0 r4 r3 r2 r1 r0 g5 g4 g3
  63. */
  64. .description = "RGB565, le",
  65. .pixelformat = V4L2_PIX_FMT_RGB565,
  66. },
  67. };
  68. #define TCM825X_NUM_CAPTURE_FORMATS ARRAY_SIZE(tcm825x_formats)
  69. /*
  70. * TCM825X register configuration for all combinations of pixel format and
  71. * image size
  72. */
  73. static const struct tcm825x_reg subqcif = { 0x20, TCM825X_PICSIZ };
  74. static const struct tcm825x_reg qcif = { 0x18, TCM825X_PICSIZ };
  75. static const struct tcm825x_reg cif = { 0x14, TCM825X_PICSIZ };
  76. static const struct tcm825x_reg qqvga = { 0x0c, TCM825X_PICSIZ };
  77. static const struct tcm825x_reg qvga = { 0x04, TCM825X_PICSIZ };
  78. static const struct tcm825x_reg vga = { 0x00, TCM825X_PICSIZ };
  79. static const struct tcm825x_reg yuv422 = { 0x00, TCM825X_PICFMT };
  80. static const struct tcm825x_reg rgb565 = { 0x02, TCM825X_PICFMT };
  81. /* Our own specific controls */
  82. #define V4L2_CID_ALC V4L2_CID_PRIVATE_BASE
  83. #define V4L2_CID_H_EDGE_EN V4L2_CID_PRIVATE_BASE + 1
  84. #define V4L2_CID_V_EDGE_EN V4L2_CID_PRIVATE_BASE + 2
  85. #define V4L2_CID_LENS V4L2_CID_PRIVATE_BASE + 3
  86. #define V4L2_CID_MAX_EXPOSURE_TIME V4L2_CID_PRIVATE_BASE + 4
  87. #define V4L2_CID_LAST_PRIV V4L2_CID_MAX_EXPOSURE_TIME
  88. /* Video controls */
  89. static struct vcontrol {
  90. struct v4l2_queryctrl qc;
  91. u16 reg;
  92. u16 start_bit;
  93. } video_control[] = {
  94. {
  95. {
  96. .id = V4L2_CID_GAIN,
  97. .type = V4L2_CTRL_TYPE_INTEGER,
  98. .name = "Gain",
  99. .minimum = 0,
  100. .maximum = 63,
  101. .step = 1,
  102. },
  103. .reg = TCM825X_AG,
  104. .start_bit = 0,
  105. },
  106. {
  107. {
  108. .id = V4L2_CID_RED_BALANCE,
  109. .type = V4L2_CTRL_TYPE_INTEGER,
  110. .name = "Red Balance",
  111. .minimum = 0,
  112. .maximum = 255,
  113. .step = 1,
  114. },
  115. .reg = TCM825X_MRG,
  116. .start_bit = 0,
  117. },
  118. {
  119. {
  120. .id = V4L2_CID_BLUE_BALANCE,
  121. .type = V4L2_CTRL_TYPE_INTEGER,
  122. .name = "Blue Balance",
  123. .minimum = 0,
  124. .maximum = 255,
  125. .step = 1,
  126. },
  127. .reg = TCM825X_MBG,
  128. .start_bit = 0,
  129. },
  130. {
  131. {
  132. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  133. .type = V4L2_CTRL_TYPE_BOOLEAN,
  134. .name = "Auto White Balance",
  135. .minimum = 0,
  136. .maximum = 1,
  137. .step = 0,
  138. },
  139. .reg = TCM825X_AWBSW,
  140. .start_bit = 7,
  141. },
  142. {
  143. {
  144. .id = V4L2_CID_EXPOSURE,
  145. .type = V4L2_CTRL_TYPE_INTEGER,
  146. .name = "Exposure Time",
  147. .minimum = 0,
  148. .maximum = 0x1fff,
  149. .step = 1,
  150. },
  151. .reg = TCM825X_ESRSPD_U,
  152. .start_bit = 0,
  153. },
  154. {
  155. {
  156. .id = V4L2_CID_HFLIP,
  157. .type = V4L2_CTRL_TYPE_BOOLEAN,
  158. .name = "Mirror Image",
  159. .minimum = 0,
  160. .maximum = 1,
  161. .step = 0,
  162. },
  163. .reg = TCM825X_H_INV,
  164. .start_bit = 6,
  165. },
  166. {
  167. {
  168. .id = V4L2_CID_VFLIP,
  169. .type = V4L2_CTRL_TYPE_BOOLEAN,
  170. .name = "Vertical Flip",
  171. .minimum = 0,
  172. .maximum = 1,
  173. .step = 0,
  174. },
  175. .reg = TCM825X_V_INV,
  176. .start_bit = 7,
  177. },
  178. /* Private controls */
  179. {
  180. {
  181. .id = V4L2_CID_ALC,
  182. .type = V4L2_CTRL_TYPE_BOOLEAN,
  183. .name = "Auto Luminance Control",
  184. .minimum = 0,
  185. .maximum = 1,
  186. .step = 0,
  187. },
  188. .reg = TCM825X_ALCSW,
  189. .start_bit = 7,
  190. },
  191. {
  192. {
  193. .id = V4L2_CID_H_EDGE_EN,
  194. .type = V4L2_CTRL_TYPE_INTEGER,
  195. .name = "Horizontal Edge Enhancement",
  196. .minimum = 0,
  197. .maximum = 0xff,
  198. .step = 1,
  199. },
  200. .reg = TCM825X_HDTG,
  201. .start_bit = 0,
  202. },
  203. {
  204. {
  205. .id = V4L2_CID_V_EDGE_EN,
  206. .type = V4L2_CTRL_TYPE_INTEGER,
  207. .name = "Vertical Edge Enhancement",
  208. .minimum = 0,
  209. .maximum = 0xff,
  210. .step = 1,
  211. },
  212. .reg = TCM825X_VDTG,
  213. .start_bit = 0,
  214. },
  215. {
  216. {
  217. .id = V4L2_CID_LENS,
  218. .type = V4L2_CTRL_TYPE_INTEGER,
  219. .name = "Lens Shading Compensation",
  220. .minimum = 0,
  221. .maximum = 0x3f,
  222. .step = 1,
  223. },
  224. .reg = TCM825X_LENS,
  225. .start_bit = 0,
  226. },
  227. {
  228. {
  229. .id = V4L2_CID_MAX_EXPOSURE_TIME,
  230. .type = V4L2_CTRL_TYPE_INTEGER,
  231. .name = "Maximum Exposure Time",
  232. .minimum = 0,
  233. .maximum = 0x3,
  234. .step = 1,
  235. },
  236. .reg = TCM825X_ESRLIM,
  237. .start_bit = 5,
  238. },
  239. };
  240. static const struct tcm825x_reg *tcm825x_siz_reg[NUM_IMAGE_SIZES] =
  241. { &subqcif, &qqvga, &qcif, &qvga, &cif, &vga };
  242. static const struct tcm825x_reg *tcm825x_fmt_reg[NUM_PIXEL_FORMATS] =
  243. { &yuv422, &rgb565 };
  244. /*
  245. * Read a value from a register in an TCM825X sensor device. The value is
  246. * returned in 'val'.
  247. * Returns zero if successful, or non-zero otherwise.
  248. */
  249. static int tcm825x_read_reg(struct i2c_client *client, int reg)
  250. {
  251. int err;
  252. struct i2c_msg msg[2];
  253. u8 reg_buf, data_buf = 0;
  254. if (!client->adapter)
  255. return -ENODEV;
  256. msg[0].addr = client->addr;
  257. msg[0].flags = 0;
  258. msg[0].len = 1;
  259. msg[0].buf = &reg_buf;
  260. msg[1].addr = client->addr;
  261. msg[1].flags = I2C_M_RD;
  262. msg[1].len = 1;
  263. msg[1].buf = &data_buf;
  264. reg_buf = reg;
  265. err = i2c_transfer(client->adapter, msg, 2);
  266. if (err < 0)
  267. return err;
  268. return data_buf;
  269. }
  270. /*
  271. * Write a value to a register in an TCM825X sensor device.
  272. * Returns zero if successful, or non-zero otherwise.
  273. */
  274. static int tcm825x_write_reg(struct i2c_client *client, u8 reg, u8 val)
  275. {
  276. int err;
  277. struct i2c_msg msg[1];
  278. unsigned char data[2];
  279. if (!client->adapter)
  280. return -ENODEV;
  281. msg->addr = client->addr;
  282. msg->flags = 0;
  283. msg->len = 2;
  284. msg->buf = data;
  285. data[0] = reg;
  286. data[1] = val;
  287. err = i2c_transfer(client->adapter, msg, 1);
  288. if (err >= 0)
  289. return 0;
  290. return err;
  291. }
  292. static int __tcm825x_write_reg_mask(struct i2c_client *client,
  293. u8 reg, u8 val, u8 mask)
  294. {
  295. int rc;
  296. /* need to do read - modify - write */
  297. rc = tcm825x_read_reg(client, reg);
  298. if (rc < 0)
  299. return rc;
  300. rc &= (~mask); /* Clear the masked bits */
  301. val &= mask; /* Enforce mask on value */
  302. val |= rc;
  303. /* write the new value to the register */
  304. rc = tcm825x_write_reg(client, reg, val);
  305. if (rc)
  306. return rc;
  307. return 0;
  308. }
  309. #define tcm825x_write_reg_mask(client, regmask, val) \
  310. __tcm825x_write_reg_mask(client, TCM825X_ADDR((regmask)), val, \
  311. TCM825X_MASK((regmask)))
  312. /*
  313. * Initialize a list of TCM825X registers.
  314. * The list of registers is terminated by the pair of values
  315. * { TCM825X_REG_TERM, TCM825X_VAL_TERM }.
  316. * Returns zero if successful, or non-zero otherwise.
  317. */
  318. static int tcm825x_write_default_regs(struct i2c_client *client,
  319. const struct tcm825x_reg *reglist)
  320. {
  321. int err;
  322. const struct tcm825x_reg *next = reglist;
  323. while (!((next->reg == TCM825X_REG_TERM)
  324. && (next->val == TCM825X_VAL_TERM))) {
  325. err = tcm825x_write_reg(client, next->reg, next->val);
  326. if (err) {
  327. dev_err(&client->dev, "register writing failed\n");
  328. return err;
  329. }
  330. next++;
  331. }
  332. return 0;
  333. }
  334. static struct vcontrol *find_vctrl(int id)
  335. {
  336. int i;
  337. if (id < V4L2_CID_BASE)
  338. return NULL;
  339. for (i = 0; i < ARRAY_SIZE(video_control); i++)
  340. if (video_control[i].qc.id == id)
  341. return &video_control[i];
  342. return NULL;
  343. }
  344. /*
  345. * Find the best match for a requested image capture size. The best match
  346. * is chosen as the nearest match that has the same number or fewer pixels
  347. * as the requested size, or the smallest image size if the requested size
  348. * has fewer pixels than the smallest image.
  349. */
  350. static enum image_size tcm825x_find_size(struct v4l2_int_device *s,
  351. unsigned int width,
  352. unsigned int height)
  353. {
  354. enum image_size isize;
  355. unsigned long pixels = width * height;
  356. struct tcm825x_sensor *sensor = s->priv;
  357. for (isize = subQCIF; isize < VGA; isize++) {
  358. if (tcm825x_sizes[isize + 1].height
  359. * tcm825x_sizes[isize + 1].width > pixels) {
  360. dev_dbg(&sensor->i2c_client->dev, "size %d\n", isize);
  361. return isize;
  362. }
  363. }
  364. dev_dbg(&sensor->i2c_client->dev, "format default VGA\n");
  365. return VGA;
  366. }
  367. /*
  368. * Configure the TCM825X for current image size, pixel format, and
  369. * frame period. fper is the frame period (in seconds) expressed as a
  370. * fraction. Returns zero if successful, or non-zero otherwise. The
  371. * actual frame period is returned in fper.
  372. */
  373. static int tcm825x_configure(struct v4l2_int_device *s)
  374. {
  375. struct tcm825x_sensor *sensor = s->priv;
  376. struct v4l2_pix_format *pix = &sensor->pix;
  377. enum image_size isize = tcm825x_find_size(s, pix->width, pix->height);
  378. struct v4l2_fract *fper = &sensor->timeperframe;
  379. enum pixel_format pfmt;
  380. int err;
  381. u32 tgt_fps;
  382. u8 val;
  383. /* common register initialization */
  384. err = tcm825x_write_default_regs(
  385. sensor->i2c_client, sensor->platform_data->default_regs());
  386. if (err)
  387. return err;
  388. /* configure image size */
  389. val = tcm825x_siz_reg[isize]->val;
  390. dev_dbg(&sensor->i2c_client->dev,
  391. "configuring image size %d\n", isize);
  392. err = tcm825x_write_reg_mask(sensor->i2c_client,
  393. tcm825x_siz_reg[isize]->reg, val);
  394. if (err)
  395. return err;
  396. /* configure pixel format */
  397. switch (pix->pixelformat) {
  398. default:
  399. case V4L2_PIX_FMT_RGB565:
  400. pfmt = RGB565;
  401. break;
  402. case V4L2_PIX_FMT_UYVY:
  403. pfmt = YUV422;
  404. break;
  405. }
  406. dev_dbg(&sensor->i2c_client->dev,
  407. "configuring pixel format %d\n", pfmt);
  408. val = tcm825x_fmt_reg[pfmt]->val;
  409. err = tcm825x_write_reg_mask(sensor->i2c_client,
  410. tcm825x_fmt_reg[pfmt]->reg, val);
  411. if (err)
  412. return err;
  413. /*
  414. * For frame rate < 15, the FPS reg (addr 0x02, bit 7) must be
  415. * set. Frame rate will be halved from the normal.
  416. */
  417. tgt_fps = fper->denominator / fper->numerator;
  418. if (tgt_fps <= HIGH_FPS_MODE_LOWER_LIMIT) {
  419. val = tcm825x_read_reg(sensor->i2c_client, 0x02);
  420. val |= 0x80;
  421. tcm825x_write_reg(sensor->i2c_client, 0x02, val);
  422. }
  423. return 0;
  424. }
  425. static int ioctl_queryctrl(struct v4l2_int_device *s,
  426. struct v4l2_queryctrl *qc)
  427. {
  428. struct vcontrol *control;
  429. control = find_vctrl(qc->id);
  430. if (control == NULL)
  431. return -EINVAL;
  432. *qc = control->qc;
  433. return 0;
  434. }
  435. static int ioctl_g_ctrl(struct v4l2_int_device *s,
  436. struct v4l2_control *vc)
  437. {
  438. struct tcm825x_sensor *sensor = s->priv;
  439. struct i2c_client *client = sensor->i2c_client;
  440. int val, r;
  441. struct vcontrol *lvc;
  442. /* exposure time is special, spread across 2 registers */
  443. if (vc->id == V4L2_CID_EXPOSURE) {
  444. int val_lower, val_upper;
  445. val_upper = tcm825x_read_reg(client,
  446. TCM825X_ADDR(TCM825X_ESRSPD_U));
  447. if (val_upper < 0)
  448. return val_upper;
  449. val_lower = tcm825x_read_reg(client,
  450. TCM825X_ADDR(TCM825X_ESRSPD_L));
  451. if (val_lower < 0)
  452. return val_lower;
  453. vc->value = ((val_upper & 0x1f) << 8) | (val_lower);
  454. return 0;
  455. }
  456. lvc = find_vctrl(vc->id);
  457. if (lvc == NULL)
  458. return -EINVAL;
  459. r = tcm825x_read_reg(client, TCM825X_ADDR(lvc->reg));
  460. if (r < 0)
  461. return r;
  462. val = r & TCM825X_MASK(lvc->reg);
  463. val >>= lvc->start_bit;
  464. if (val < 0)
  465. return val;
  466. if (vc->id == V4L2_CID_HFLIP || vc->id == V4L2_CID_VFLIP)
  467. val ^= sensor->platform_data->is_upside_down();
  468. vc->value = val;
  469. return 0;
  470. }
  471. static int ioctl_s_ctrl(struct v4l2_int_device *s,
  472. struct v4l2_control *vc)
  473. {
  474. struct tcm825x_sensor *sensor = s->priv;
  475. struct i2c_client *client = sensor->i2c_client;
  476. struct vcontrol *lvc;
  477. int val = vc->value;
  478. /* exposure time is special, spread across 2 registers */
  479. if (vc->id == V4L2_CID_EXPOSURE) {
  480. int val_lower, val_upper;
  481. val_lower = val & TCM825X_MASK(TCM825X_ESRSPD_L);
  482. val_upper = (val >> 8) & TCM825X_MASK(TCM825X_ESRSPD_U);
  483. if (tcm825x_write_reg_mask(client,
  484. TCM825X_ESRSPD_U, val_upper))
  485. return -EIO;
  486. if (tcm825x_write_reg_mask(client,
  487. TCM825X_ESRSPD_L, val_lower))
  488. return -EIO;
  489. return 0;
  490. }
  491. lvc = find_vctrl(vc->id);
  492. if (lvc == NULL)
  493. return -EINVAL;
  494. if (vc->id == V4L2_CID_HFLIP || vc->id == V4L2_CID_VFLIP)
  495. val ^= sensor->platform_data->is_upside_down();
  496. val = val << lvc->start_bit;
  497. if (tcm825x_write_reg_mask(client, lvc->reg, val))
  498. return -EIO;
  499. return 0;
  500. }
  501. static int ioctl_enum_fmt_cap(struct v4l2_int_device *s,
  502. struct v4l2_fmtdesc *fmt)
  503. {
  504. int index = fmt->index;
  505. switch (fmt->type) {
  506. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  507. if (index >= TCM825X_NUM_CAPTURE_FORMATS)
  508. return -EINVAL;
  509. break;
  510. default:
  511. return -EINVAL;
  512. }
  513. fmt->flags = tcm825x_formats[index].flags;
  514. strlcpy(fmt->description, tcm825x_formats[index].description,
  515. sizeof(fmt->description));
  516. fmt->pixelformat = tcm825x_formats[index].pixelformat;
  517. return 0;
  518. }
  519. static int ioctl_try_fmt_cap(struct v4l2_int_device *s,
  520. struct v4l2_format *f)
  521. {
  522. struct tcm825x_sensor *sensor = s->priv;
  523. enum image_size isize;
  524. int ifmt;
  525. struct v4l2_pix_format *pix = &f->fmt.pix;
  526. isize = tcm825x_find_size(s, pix->width, pix->height);
  527. dev_dbg(&sensor->i2c_client->dev, "isize = %d num_capture = %lu\n",
  528. isize, (unsigned long)TCM825X_NUM_CAPTURE_FORMATS);
  529. pix->width = tcm825x_sizes[isize].width;
  530. pix->height = tcm825x_sizes[isize].height;
  531. for (ifmt = 0; ifmt < TCM825X_NUM_CAPTURE_FORMATS; ifmt++)
  532. if (pix->pixelformat == tcm825x_formats[ifmt].pixelformat)
  533. break;
  534. if (ifmt == TCM825X_NUM_CAPTURE_FORMATS)
  535. ifmt = 0; /* Default = YUV 4:2:2 */
  536. pix->pixelformat = tcm825x_formats[ifmt].pixelformat;
  537. pix->field = V4L2_FIELD_NONE;
  538. pix->bytesperline = pix->width * TCM825X_BYTES_PER_PIXEL;
  539. pix->sizeimage = pix->bytesperline * pix->height;
  540. pix->priv = 0;
  541. dev_dbg(&sensor->i2c_client->dev, "format = 0x%08x\n",
  542. pix->pixelformat);
  543. switch (pix->pixelformat) {
  544. case V4L2_PIX_FMT_UYVY:
  545. default:
  546. pix->colorspace = V4L2_COLORSPACE_JPEG;
  547. break;
  548. case V4L2_PIX_FMT_RGB565:
  549. pix->colorspace = V4L2_COLORSPACE_SRGB;
  550. break;
  551. }
  552. return 0;
  553. }
  554. static int ioctl_s_fmt_cap(struct v4l2_int_device *s,
  555. struct v4l2_format *f)
  556. {
  557. struct tcm825x_sensor *sensor = s->priv;
  558. struct v4l2_pix_format *pix = &f->fmt.pix;
  559. int rval;
  560. rval = ioctl_try_fmt_cap(s, f);
  561. if (rval)
  562. return rval;
  563. rval = tcm825x_configure(s);
  564. sensor->pix = *pix;
  565. return rval;
  566. }
  567. static int ioctl_g_fmt_cap(struct v4l2_int_device *s,
  568. struct v4l2_format *f)
  569. {
  570. struct tcm825x_sensor *sensor = s->priv;
  571. f->fmt.pix = sensor->pix;
  572. return 0;
  573. }
  574. static int ioctl_g_parm(struct v4l2_int_device *s,
  575. struct v4l2_streamparm *a)
  576. {
  577. struct tcm825x_sensor *sensor = s->priv;
  578. struct v4l2_captureparm *cparm = &a->parm.capture;
  579. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  580. return -EINVAL;
  581. memset(a, 0, sizeof(*a));
  582. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  583. cparm->capability = V4L2_CAP_TIMEPERFRAME;
  584. cparm->timeperframe = sensor->timeperframe;
  585. return 0;
  586. }
  587. static int ioctl_s_parm(struct v4l2_int_device *s,
  588. struct v4l2_streamparm *a)
  589. {
  590. struct tcm825x_sensor *sensor = s->priv;
  591. struct v4l2_fract *timeperframe = &a->parm.capture.timeperframe;
  592. u32 tgt_fps; /* target frames per secound */
  593. int rval;
  594. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  595. return -EINVAL;
  596. if ((timeperframe->numerator == 0)
  597. || (timeperframe->denominator == 0)) {
  598. timeperframe->denominator = DEFAULT_FPS;
  599. timeperframe->numerator = 1;
  600. }
  601. tgt_fps = timeperframe->denominator / timeperframe->numerator;
  602. if (tgt_fps > MAX_FPS) {
  603. timeperframe->denominator = MAX_FPS;
  604. timeperframe->numerator = 1;
  605. } else if (tgt_fps < MIN_FPS) {
  606. timeperframe->denominator = MIN_FPS;
  607. timeperframe->numerator = 1;
  608. }
  609. sensor->timeperframe = *timeperframe;
  610. rval = tcm825x_configure(s);
  611. return rval;
  612. }
  613. static int ioctl_s_power(struct v4l2_int_device *s, int on)
  614. {
  615. struct tcm825x_sensor *sensor = s->priv;
  616. return sensor->platform_data->power_set(on);
  617. }
  618. /*
  619. * Given the image capture format in pix, the nominal frame period in
  620. * timeperframe, calculate the required xclk frequency.
  621. *
  622. * TCM825X input frequency characteristics are:
  623. * Minimum 11.9 MHz, Typical 24.57 MHz and maximum 25/27 MHz
  624. */
  625. static int ioctl_g_ifparm(struct v4l2_int_device *s, struct v4l2_ifparm *p)
  626. {
  627. struct tcm825x_sensor *sensor = s->priv;
  628. struct v4l2_fract *timeperframe = &sensor->timeperframe;
  629. u32 tgt_xclk; /* target xclk */
  630. u32 tgt_fps; /* target frames per secound */
  631. int rval;
  632. rval = sensor->platform_data->ifparm(p);
  633. if (rval)
  634. return rval;
  635. tgt_fps = timeperframe->denominator / timeperframe->numerator;
  636. tgt_xclk = (tgt_fps <= HIGH_FPS_MODE_LOWER_LIMIT) ?
  637. (2457 * tgt_fps) / MAX_HALF_FPS :
  638. (2457 * tgt_fps) / MAX_FPS;
  639. tgt_xclk *= 10000;
  640. tgt_xclk = min(tgt_xclk, (u32)TCM825X_XCLK_MAX);
  641. tgt_xclk = max(tgt_xclk, (u32)TCM825X_XCLK_MIN);
  642. p->u.bt656.clock_curr = tgt_xclk;
  643. return 0;
  644. }
  645. static int ioctl_g_needs_reset(struct v4l2_int_device *s, void *buf)
  646. {
  647. struct tcm825x_sensor *sensor = s->priv;
  648. return sensor->platform_data->needs_reset(s, buf, &sensor->pix);
  649. }
  650. static int ioctl_reset(struct v4l2_int_device *s)
  651. {
  652. return -EBUSY;
  653. }
  654. static int ioctl_init(struct v4l2_int_device *s)
  655. {
  656. return tcm825x_configure(s);
  657. }
  658. static int ioctl_dev_exit(struct v4l2_int_device *s)
  659. {
  660. return 0;
  661. }
  662. static int ioctl_dev_init(struct v4l2_int_device *s)
  663. {
  664. struct tcm825x_sensor *sensor = s->priv;
  665. int r;
  666. r = tcm825x_read_reg(sensor->i2c_client, 0x01);
  667. if (r < 0)
  668. return r;
  669. if (r == 0) {
  670. dev_err(&sensor->i2c_client->dev, "device not detected\n");
  671. return -EIO;
  672. }
  673. return 0;
  674. }
  675. static struct v4l2_int_ioctl_desc tcm825x_ioctl_desc[] = {
  676. { vidioc_int_dev_init_num,
  677. (v4l2_int_ioctl_func *)ioctl_dev_init },
  678. { vidioc_int_dev_exit_num,
  679. (v4l2_int_ioctl_func *)ioctl_dev_exit },
  680. { vidioc_int_s_power_num,
  681. (v4l2_int_ioctl_func *)ioctl_s_power },
  682. { vidioc_int_g_ifparm_num,
  683. (v4l2_int_ioctl_func *)ioctl_g_ifparm },
  684. { vidioc_int_g_needs_reset_num,
  685. (v4l2_int_ioctl_func *)ioctl_g_needs_reset },
  686. { vidioc_int_reset_num,
  687. (v4l2_int_ioctl_func *)ioctl_reset },
  688. { vidioc_int_init_num,
  689. (v4l2_int_ioctl_func *)ioctl_init },
  690. { vidioc_int_enum_fmt_cap_num,
  691. (v4l2_int_ioctl_func *)ioctl_enum_fmt_cap },
  692. { vidioc_int_try_fmt_cap_num,
  693. (v4l2_int_ioctl_func *)ioctl_try_fmt_cap },
  694. { vidioc_int_g_fmt_cap_num,
  695. (v4l2_int_ioctl_func *)ioctl_g_fmt_cap },
  696. { vidioc_int_s_fmt_cap_num,
  697. (v4l2_int_ioctl_func *)ioctl_s_fmt_cap },
  698. { vidioc_int_g_parm_num,
  699. (v4l2_int_ioctl_func *)ioctl_g_parm },
  700. { vidioc_int_s_parm_num,
  701. (v4l2_int_ioctl_func *)ioctl_s_parm },
  702. { vidioc_int_queryctrl_num,
  703. (v4l2_int_ioctl_func *)ioctl_queryctrl },
  704. { vidioc_int_g_ctrl_num,
  705. (v4l2_int_ioctl_func *)ioctl_g_ctrl },
  706. { vidioc_int_s_ctrl_num,
  707. (v4l2_int_ioctl_func *)ioctl_s_ctrl },
  708. };
  709. static struct v4l2_int_slave tcm825x_slave = {
  710. .ioctls = tcm825x_ioctl_desc,
  711. .num_ioctls = ARRAY_SIZE(tcm825x_ioctl_desc),
  712. };
  713. static struct tcm825x_sensor tcm825x;
  714. static struct v4l2_int_device tcm825x_int_device = {
  715. .module = THIS_MODULE,
  716. .name = TCM825X_NAME,
  717. .priv = &tcm825x,
  718. .type = v4l2_int_type_slave,
  719. .u = {
  720. .slave = &tcm825x_slave,
  721. },
  722. };
  723. static int tcm825x_probe(struct i2c_client *client,
  724. const struct i2c_device_id *did)
  725. {
  726. struct tcm825x_sensor *sensor = &tcm825x;
  727. if (i2c_get_clientdata(client))
  728. return -EBUSY;
  729. sensor->platform_data = client->dev.platform_data;
  730. if (sensor->platform_data == NULL
  731. || !sensor->platform_data->is_okay())
  732. return -ENODEV;
  733. sensor->v4l2_int_device = &tcm825x_int_device;
  734. sensor->i2c_client = client;
  735. i2c_set_clientdata(client, sensor);
  736. /* Make the default capture format QVGA RGB565 */
  737. sensor->pix.width = tcm825x_sizes[QVGA].width;
  738. sensor->pix.height = tcm825x_sizes[QVGA].height;
  739. sensor->pix.pixelformat = V4L2_PIX_FMT_RGB565;
  740. return v4l2_int_device_register(sensor->v4l2_int_device);
  741. }
  742. static int tcm825x_remove(struct i2c_client *client)
  743. {
  744. struct tcm825x_sensor *sensor = i2c_get_clientdata(client);
  745. if (!client->adapter)
  746. return -ENODEV; /* our client isn't attached */
  747. v4l2_int_device_unregister(sensor->v4l2_int_device);
  748. return 0;
  749. }
  750. static const struct i2c_device_id tcm825x_id[] = {
  751. { "tcm825x", 0 },
  752. { }
  753. };
  754. MODULE_DEVICE_TABLE(i2c, tcm825x_id);
  755. static struct i2c_driver tcm825x_i2c_driver = {
  756. .driver = {
  757. .name = TCM825X_NAME,
  758. },
  759. .probe = tcm825x_probe,
  760. .remove = tcm825x_remove,
  761. .id_table = tcm825x_id,
  762. };
  763. static struct tcm825x_sensor tcm825x = {
  764. .timeperframe = {
  765. .numerator = 1,
  766. .denominator = DEFAULT_FPS,
  767. },
  768. };
  769. static int __init tcm825x_init(void)
  770. {
  771. int rval;
  772. rval = i2c_add_driver(&tcm825x_i2c_driver);
  773. if (rval)
  774. printk(KERN_INFO "%s: failed registering " TCM825X_NAME "\n",
  775. __func__);
  776. return rval;
  777. }
  778. static void __exit tcm825x_exit(void)
  779. {
  780. i2c_del_driver(&tcm825x_i2c_driver);
  781. }
  782. /*
  783. * FIXME: Menelaus isn't ready (?) at module_init stage, so use
  784. * late_initcall for now.
  785. */
  786. late_initcall(tcm825x_init);
  787. module_exit(tcm825x_exit);
  788. MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
  789. MODULE_DESCRIPTION("TCM825x camera sensor driver");
  790. MODULE_LICENSE("GPL");