tcm825x.c 22 KB

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