mt9m032.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*
  2. * Driver for MT9M032 CMOS Image Sensor from Micron
  3. *
  4. * Copyright (C) 2010-2011 Lund Engineering
  5. * Contact: Gil Lund <gwlund@lundeng.com>
  6. * Author: Martin Hostettler <martin@neutronstar.dyndns.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. */
  22. #include <linux/delay.h>
  23. #include <linux/i2c.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/math64.h>
  27. #include <linux/module.h>
  28. #include <linux/mutex.h>
  29. #include <linux/slab.h>
  30. #include <linux/v4l2-mediabus.h>
  31. #include <media/media-entity.h>
  32. #include <media/mt9m032.h>
  33. #include <media/v4l2-ctrls.h>
  34. #include <media/v4l2-device.h>
  35. #include <media/v4l2-subdev.h>
  36. #include "aptina-pll.h"
  37. /*
  38. * width and height include active boundary and black parts
  39. *
  40. * column 0- 15 active boundary
  41. * column 16-1455 image
  42. * column 1456-1471 active boundary
  43. * column 1472-1599 black
  44. *
  45. * row 0- 51 black
  46. * row 53- 59 active boundary
  47. * row 60-1139 image
  48. * row 1140-1147 active boundary
  49. * row 1148-1151 black
  50. */
  51. #define MT9M032_PIXEL_ARRAY_WIDTH 1600
  52. #define MT9M032_PIXEL_ARRAY_HEIGHT 1152
  53. #define MT9M032_CHIP_VERSION 0x00
  54. #define MT9M032_CHIP_VERSION_VALUE 0x1402
  55. #define MT9M032_ROW_START 0x01
  56. #define MT9M032_ROW_START_MIN 0
  57. #define MT9M032_ROW_START_MAX 1152
  58. #define MT9M032_ROW_START_DEF 60
  59. #define MT9M032_COLUMN_START 0x02
  60. #define MT9M032_COLUMN_START_MIN 0
  61. #define MT9M032_COLUMN_START_MAX 1600
  62. #define MT9M032_COLUMN_START_DEF 16
  63. #define MT9M032_ROW_SIZE 0x03
  64. #define MT9M032_ROW_SIZE_MIN 32
  65. #define MT9M032_ROW_SIZE_MAX 1152
  66. #define MT9M032_ROW_SIZE_DEF 1080
  67. #define MT9M032_COLUMN_SIZE 0x04
  68. #define MT9M032_COLUMN_SIZE_MIN 32
  69. #define MT9M032_COLUMN_SIZE_MAX 1600
  70. #define MT9M032_COLUMN_SIZE_DEF 1440
  71. #define MT9M032_HBLANK 0x05
  72. #define MT9M032_VBLANK 0x06
  73. #define MT9M032_VBLANK_MAX 0x7ff
  74. #define MT9M032_SHUTTER_WIDTH_HIGH 0x08
  75. #define MT9M032_SHUTTER_WIDTH_LOW 0x09
  76. #define MT9M032_SHUTTER_WIDTH_MIN 1
  77. #define MT9M032_SHUTTER_WIDTH_MAX 1048575
  78. #define MT9M032_SHUTTER_WIDTH_DEF 1943
  79. #define MT9M032_PIX_CLK_CTRL 0x0a
  80. #define MT9M032_PIX_CLK_CTRL_INV_PIXCLK 0x8000
  81. #define MT9M032_RESTART 0x0b
  82. #define MT9M032_RESET 0x0d
  83. #define MT9M032_PLL_CONFIG1 0x11
  84. #define MT9M032_PLL_CONFIG1_OUTDIV_MASK 0x3f
  85. #define MT9M032_PLL_CONFIG1_MUL_SHIFT 8
  86. #define MT9M032_READ_MODE1 0x1e
  87. #define MT9M032_READ_MODE2 0x20
  88. #define MT9M032_READ_MODE2_VFLIP_SHIFT 15
  89. #define MT9M032_READ_MODE2_HFLIP_SHIFT 14
  90. #define MT9M032_READ_MODE2_ROW_BLC 0x40
  91. #define MT9M032_GAIN_GREEN1 0x2b
  92. #define MT9M032_GAIN_BLUE 0x2c
  93. #define MT9M032_GAIN_RED 0x2d
  94. #define MT9M032_GAIN_GREEN2 0x2e
  95. /* write only */
  96. #define MT9M032_GAIN_ALL 0x35
  97. #define MT9M032_GAIN_DIGITAL_MASK 0x7f
  98. #define MT9M032_GAIN_DIGITAL_SHIFT 8
  99. #define MT9M032_GAIN_AMUL_SHIFT 6
  100. #define MT9M032_GAIN_ANALOG_MASK 0x3f
  101. #define MT9M032_FORMATTER1 0x9e
  102. #define MT9M032_FORMATTER2 0x9f
  103. #define MT9M032_FORMATTER2_DOUT_EN 0x1000
  104. #define MT9M032_FORMATTER2_PIXCLK_EN 0x2000
  105. /*
  106. * The available MT9M032 datasheet is missing documentation for register 0x10
  107. * MT9P031 seems to be close enough, so use constants from that datasheet for
  108. * now.
  109. * But keep the name MT9P031 to remind us, that this isn't really confirmed
  110. * for this sensor.
  111. */
  112. #define MT9P031_PLL_CONTROL 0x10
  113. #define MT9P031_PLL_CONTROL_PWROFF 0x0050
  114. #define MT9P031_PLL_CONTROL_PWRON 0x0051
  115. #define MT9P031_PLL_CONTROL_USEPLL 0x0052
  116. #define MT9P031_PLL_CONFIG2 0x11
  117. #define MT9P031_PLL_CONFIG2_P1_DIV_MASK 0x1f
  118. struct mt9m032 {
  119. struct v4l2_subdev subdev;
  120. struct media_pad pad;
  121. struct mt9m032_platform_data *pdata;
  122. unsigned int pix_clock;
  123. struct v4l2_ctrl_handler ctrls;
  124. struct {
  125. struct v4l2_ctrl *hflip;
  126. struct v4l2_ctrl *vflip;
  127. };
  128. struct mutex lock; /* Protects streaming, format, interval and crop */
  129. bool streaming;
  130. struct v4l2_mbus_framefmt format;
  131. struct v4l2_rect crop;
  132. struct v4l2_fract frame_interval;
  133. };
  134. #define to_mt9m032(sd) container_of(sd, struct mt9m032, subdev)
  135. #define to_dev(sensor) \
  136. (&((struct i2c_client *)v4l2_get_subdevdata(&(sensor)->subdev))->dev)
  137. static int mt9m032_read(struct i2c_client *client, u8 reg)
  138. {
  139. return i2c_smbus_read_word_swapped(client, reg);
  140. }
  141. static int mt9m032_write(struct i2c_client *client, u8 reg, const u16 data)
  142. {
  143. return i2c_smbus_write_word_swapped(client, reg, data);
  144. }
  145. static u32 mt9m032_row_time(struct mt9m032 *sensor, unsigned int width)
  146. {
  147. unsigned int effective_width;
  148. u32 ns;
  149. effective_width = width + 716; /* empirical value */
  150. ns = div_u64(1000000000ULL * effective_width, sensor->pix_clock);
  151. dev_dbg(to_dev(sensor), "MT9M032 line time: %u ns\n", ns);
  152. return ns;
  153. }
  154. static int mt9m032_update_timing(struct mt9m032 *sensor,
  155. struct v4l2_fract *interval)
  156. {
  157. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  158. struct v4l2_rect *crop = &sensor->crop;
  159. unsigned int min_vblank;
  160. unsigned int vblank;
  161. u32 row_time;
  162. if (!interval)
  163. interval = &sensor->frame_interval;
  164. row_time = mt9m032_row_time(sensor, crop->width);
  165. vblank = div_u64(1000000000ULL * interval->numerator,
  166. (u64)row_time * interval->denominator)
  167. - crop->height;
  168. if (vblank > MT9M032_VBLANK_MAX) {
  169. /* hardware limits to 11 bit values */
  170. interval->denominator = 1000;
  171. interval->numerator =
  172. div_u64((crop->height + MT9M032_VBLANK_MAX) *
  173. (u64)row_time * interval->denominator,
  174. 1000000000ULL);
  175. vblank = div_u64(1000000000ULL * interval->numerator,
  176. (u64)row_time * interval->denominator)
  177. - crop->height;
  178. }
  179. /* enforce minimal 1.6ms blanking time. */
  180. min_vblank = 1600000 / row_time;
  181. vblank = clamp_t(unsigned int, vblank, min_vblank, MT9M032_VBLANK_MAX);
  182. return mt9m032_write(client, MT9M032_VBLANK, vblank);
  183. }
  184. static int mt9m032_update_geom_timing(struct mt9m032 *sensor)
  185. {
  186. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  187. int ret;
  188. ret = mt9m032_write(client, MT9M032_COLUMN_SIZE,
  189. sensor->crop.width - 1);
  190. if (!ret)
  191. ret = mt9m032_write(client, MT9M032_ROW_SIZE,
  192. sensor->crop.height - 1);
  193. if (!ret)
  194. ret = mt9m032_write(client, MT9M032_COLUMN_START,
  195. sensor->crop.left);
  196. if (!ret)
  197. ret = mt9m032_write(client, MT9M032_ROW_START,
  198. sensor->crop.top);
  199. if (!ret)
  200. ret = mt9m032_update_timing(sensor, NULL);
  201. return ret;
  202. }
  203. static int update_formatter2(struct mt9m032 *sensor, bool streaming)
  204. {
  205. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  206. u16 reg_val = MT9M032_FORMATTER2_DOUT_EN
  207. | 0x0070; /* parts reserved! */
  208. /* possibly for changing to 14-bit mode */
  209. if (streaming)
  210. reg_val |= MT9M032_FORMATTER2_PIXCLK_EN; /* pixclock enable */
  211. return mt9m032_write(client, MT9M032_FORMATTER2, reg_val);
  212. }
  213. static int mt9m032_setup_pll(struct mt9m032 *sensor)
  214. {
  215. static const struct aptina_pll_limits limits = {
  216. .ext_clock_min = 8000000,
  217. .ext_clock_max = 16500000,
  218. .int_clock_min = 2000000,
  219. .int_clock_max = 24000000,
  220. .out_clock_min = 322000000,
  221. .out_clock_max = 693000000,
  222. .pix_clock_max = 99000000,
  223. .n_min = 1,
  224. .n_max = 64,
  225. .m_min = 16,
  226. .m_max = 255,
  227. .p1_min = 1,
  228. .p1_max = 128,
  229. };
  230. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  231. struct mt9m032_platform_data *pdata = sensor->pdata;
  232. struct aptina_pll pll;
  233. int ret;
  234. pll.ext_clock = pdata->ext_clock;
  235. pll.pix_clock = pdata->pix_clock;
  236. ret = aptina_pll_calculate(&client->dev, &limits, &pll);
  237. if (ret < 0)
  238. return ret;
  239. sensor->pix_clock = pdata->pix_clock;
  240. ret = mt9m032_write(client, MT9M032_PLL_CONFIG1,
  241. (pll.m << MT9M032_PLL_CONFIG1_MUL_SHIFT)
  242. | (pll.p1 - 1));
  243. if (!ret)
  244. ret = mt9m032_write(client, MT9P031_PLL_CONFIG2, pll.n - 1);
  245. if (!ret)
  246. ret = mt9m032_write(client, MT9P031_PLL_CONTROL,
  247. MT9P031_PLL_CONTROL_PWRON |
  248. MT9P031_PLL_CONTROL_USEPLL);
  249. if (!ret) /* more reserved, Continuous, Master Mode */
  250. ret = mt9m032_write(client, MT9M032_READ_MODE1, 0x8006);
  251. if (!ret) /* Set 14-bit mode, select 7 divider */
  252. ret = mt9m032_write(client, MT9M032_FORMATTER1, 0x111e);
  253. return ret;
  254. }
  255. /* -----------------------------------------------------------------------------
  256. * Subdev pad operations
  257. */
  258. static int mt9m032_enum_mbus_code(struct v4l2_subdev *subdev,
  259. struct v4l2_subdev_fh *fh,
  260. struct v4l2_subdev_mbus_code_enum *code)
  261. {
  262. if (code->index != 0)
  263. return -EINVAL;
  264. code->code = V4L2_MBUS_FMT_Y8_1X8;
  265. return 0;
  266. }
  267. static int mt9m032_enum_frame_size(struct v4l2_subdev *subdev,
  268. struct v4l2_subdev_fh *fh,
  269. struct v4l2_subdev_frame_size_enum *fse)
  270. {
  271. if (fse->index != 0 || fse->code != V4L2_MBUS_FMT_Y8_1X8)
  272. return -EINVAL;
  273. fse->min_width = MT9M032_COLUMN_SIZE_DEF;
  274. fse->max_width = MT9M032_COLUMN_SIZE_DEF;
  275. fse->min_height = MT9M032_ROW_SIZE_DEF;
  276. fse->max_height = MT9M032_ROW_SIZE_DEF;
  277. return 0;
  278. }
  279. /**
  280. * __mt9m032_get_pad_crop() - get crop rect
  281. * @sensor: pointer to the sensor struct
  282. * @fh: file handle for getting the try crop rect from
  283. * @which: select try or active crop rect
  284. *
  285. * Returns a pointer the current active or fh relative try crop rect
  286. */
  287. static struct v4l2_rect *
  288. __mt9m032_get_pad_crop(struct mt9m032 *sensor, struct v4l2_subdev_fh *fh,
  289. enum v4l2_subdev_format_whence which)
  290. {
  291. switch (which) {
  292. case V4L2_SUBDEV_FORMAT_TRY:
  293. return v4l2_subdev_get_try_crop(fh, 0);
  294. case V4L2_SUBDEV_FORMAT_ACTIVE:
  295. return &sensor->crop;
  296. default:
  297. return NULL;
  298. }
  299. }
  300. /**
  301. * __mt9m032_get_pad_format() - get format
  302. * @sensor: pointer to the sensor struct
  303. * @fh: file handle for getting the try format from
  304. * @which: select try or active format
  305. *
  306. * Returns a pointer the current active or fh relative try format
  307. */
  308. static struct v4l2_mbus_framefmt *
  309. __mt9m032_get_pad_format(struct mt9m032 *sensor, struct v4l2_subdev_fh *fh,
  310. enum v4l2_subdev_format_whence which)
  311. {
  312. switch (which) {
  313. case V4L2_SUBDEV_FORMAT_TRY:
  314. return v4l2_subdev_get_try_format(fh, 0);
  315. case V4L2_SUBDEV_FORMAT_ACTIVE:
  316. return &sensor->format;
  317. default:
  318. return NULL;
  319. }
  320. }
  321. static int mt9m032_get_pad_format(struct v4l2_subdev *subdev,
  322. struct v4l2_subdev_fh *fh,
  323. struct v4l2_subdev_format *fmt)
  324. {
  325. struct mt9m032 *sensor = to_mt9m032(subdev);
  326. mutex_lock(&sensor->lock);
  327. fmt->format = *__mt9m032_get_pad_format(sensor, fh, fmt->which);
  328. mutex_unlock(&sensor->lock);
  329. return 0;
  330. }
  331. static int mt9m032_set_pad_format(struct v4l2_subdev *subdev,
  332. struct v4l2_subdev_fh *fh,
  333. struct v4l2_subdev_format *fmt)
  334. {
  335. struct mt9m032 *sensor = to_mt9m032(subdev);
  336. int ret;
  337. mutex_lock(&sensor->lock);
  338. if (sensor->streaming && fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  339. ret = -EBUSY;
  340. goto done;
  341. }
  342. /* Scaling is not supported, the format is thus fixed. */
  343. fmt->format = *__mt9m032_get_pad_format(sensor, fh, fmt->which);
  344. ret = 0;
  345. done:
  346. mutex_unlock(&sensor->lock);
  347. return ret;
  348. }
  349. static int mt9m032_get_pad_crop(struct v4l2_subdev *subdev,
  350. struct v4l2_subdev_fh *fh,
  351. struct v4l2_subdev_crop *crop)
  352. {
  353. struct mt9m032 *sensor = to_mt9m032(subdev);
  354. mutex_lock(&sensor->lock);
  355. crop->rect = *__mt9m032_get_pad_crop(sensor, fh, crop->which);
  356. mutex_unlock(&sensor->lock);
  357. return 0;
  358. }
  359. static int mt9m032_set_pad_crop(struct v4l2_subdev *subdev,
  360. struct v4l2_subdev_fh *fh,
  361. struct v4l2_subdev_crop *crop)
  362. {
  363. struct mt9m032 *sensor = to_mt9m032(subdev);
  364. struct v4l2_mbus_framefmt *format;
  365. struct v4l2_rect *__crop;
  366. struct v4l2_rect rect;
  367. int ret = 0;
  368. mutex_lock(&sensor->lock);
  369. if (sensor->streaming && crop->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  370. ret = -EBUSY;
  371. goto done;
  372. }
  373. /* Clamp the crop rectangle boundaries and align them to a multiple of 2
  374. * pixels to ensure a GRBG Bayer pattern.
  375. */
  376. rect.left = clamp(ALIGN(crop->rect.left, 2), MT9M032_COLUMN_START_MIN,
  377. MT9M032_COLUMN_START_MAX);
  378. rect.top = clamp(ALIGN(crop->rect.top, 2), MT9M032_ROW_START_MIN,
  379. MT9M032_ROW_START_MAX);
  380. rect.width = clamp(ALIGN(crop->rect.width, 2), MT9M032_COLUMN_SIZE_MIN,
  381. MT9M032_COLUMN_SIZE_MAX);
  382. rect.height = clamp(ALIGN(crop->rect.height, 2), MT9M032_ROW_SIZE_MIN,
  383. MT9M032_ROW_SIZE_MAX);
  384. rect.width = min(rect.width, MT9M032_PIXEL_ARRAY_WIDTH - rect.left);
  385. rect.height = min(rect.height, MT9M032_PIXEL_ARRAY_HEIGHT - rect.top);
  386. __crop = __mt9m032_get_pad_crop(sensor, fh, crop->which);
  387. if (rect.width != __crop->width || rect.height != __crop->height) {
  388. /* Reset the output image size if the crop rectangle size has
  389. * been modified.
  390. */
  391. format = __mt9m032_get_pad_format(sensor, fh, crop->which);
  392. format->width = rect.width;
  393. format->height = rect.height;
  394. }
  395. *__crop = rect;
  396. crop->rect = rect;
  397. if (crop->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  398. ret = mt9m032_update_geom_timing(sensor);
  399. done:
  400. mutex_unlock(&sensor->lock);
  401. return ret;
  402. }
  403. static int mt9m032_get_frame_interval(struct v4l2_subdev *subdev,
  404. struct v4l2_subdev_frame_interval *fi)
  405. {
  406. struct mt9m032 *sensor = to_mt9m032(subdev);
  407. mutex_lock(&sensor->lock);
  408. memset(fi, 0, sizeof(*fi));
  409. fi->interval = sensor->frame_interval;
  410. mutex_unlock(&sensor->lock);
  411. return 0;
  412. }
  413. static int mt9m032_set_frame_interval(struct v4l2_subdev *subdev,
  414. struct v4l2_subdev_frame_interval *fi)
  415. {
  416. struct mt9m032 *sensor = to_mt9m032(subdev);
  417. int ret;
  418. mutex_lock(&sensor->lock);
  419. if (sensor->streaming) {
  420. ret = -EBUSY;
  421. goto done;
  422. }
  423. /* Avoid divisions by 0. */
  424. if (fi->interval.denominator == 0)
  425. fi->interval.denominator = 1;
  426. ret = mt9m032_update_timing(sensor, &fi->interval);
  427. if (!ret)
  428. sensor->frame_interval = fi->interval;
  429. done:
  430. mutex_unlock(&sensor->lock);
  431. return ret;
  432. }
  433. static int mt9m032_s_stream(struct v4l2_subdev *subdev, int streaming)
  434. {
  435. struct mt9m032 *sensor = to_mt9m032(subdev);
  436. int ret;
  437. mutex_lock(&sensor->lock);
  438. ret = update_formatter2(sensor, streaming);
  439. if (!ret)
  440. sensor->streaming = streaming;
  441. mutex_unlock(&sensor->lock);
  442. return ret;
  443. }
  444. /* -----------------------------------------------------------------------------
  445. * V4L2 subdev core operations
  446. */
  447. #ifdef CONFIG_VIDEO_ADV_DEBUG
  448. static int mt9m032_g_register(struct v4l2_subdev *sd,
  449. struct v4l2_dbg_register *reg)
  450. {
  451. struct mt9m032 *sensor = to_mt9m032(sd);
  452. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  453. int val;
  454. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  455. return -EINVAL;
  456. if (reg->match.addr != client->addr)
  457. return -ENODEV;
  458. val = mt9m032_read(client, reg->reg);
  459. if (val < 0)
  460. return -EIO;
  461. reg->size = 2;
  462. reg->val = val;
  463. return 0;
  464. }
  465. static int mt9m032_s_register(struct v4l2_subdev *sd,
  466. struct v4l2_dbg_register *reg)
  467. {
  468. struct mt9m032 *sensor = to_mt9m032(sd);
  469. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  470. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  471. return -EINVAL;
  472. if (reg->match.addr != client->addr)
  473. return -ENODEV;
  474. return mt9m032_write(client, reg->reg, reg->val);
  475. }
  476. #endif
  477. /* -----------------------------------------------------------------------------
  478. * V4L2 subdev control operations
  479. */
  480. static int update_read_mode2(struct mt9m032 *sensor, bool vflip, bool hflip)
  481. {
  482. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  483. int reg_val = (vflip << MT9M032_READ_MODE2_VFLIP_SHIFT)
  484. | (hflip << MT9M032_READ_MODE2_HFLIP_SHIFT)
  485. | MT9M032_READ_MODE2_ROW_BLC
  486. | 0x0007;
  487. return mt9m032_write(client, MT9M032_READ_MODE2, reg_val);
  488. }
  489. static int mt9m032_set_gain(struct mt9m032 *sensor, s32 val)
  490. {
  491. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  492. int digital_gain_val; /* in 1/8th (0..127) */
  493. int analog_mul; /* 0 or 1 */
  494. int analog_gain_val; /* in 1/16th. (0..63) */
  495. u16 reg_val;
  496. digital_gain_val = 51; /* from setup example */
  497. if (val < 63) {
  498. analog_mul = 0;
  499. analog_gain_val = val;
  500. } else {
  501. analog_mul = 1;
  502. analog_gain_val = val / 2;
  503. }
  504. /* a_gain = (1 + analog_mul) + (analog_gain_val + 1) / 16 */
  505. /* overall_gain = a_gain * (1 + digital_gain_val / 8) */
  506. reg_val = ((digital_gain_val & MT9M032_GAIN_DIGITAL_MASK)
  507. << MT9M032_GAIN_DIGITAL_SHIFT)
  508. | ((analog_mul & 1) << MT9M032_GAIN_AMUL_SHIFT)
  509. | (analog_gain_val & MT9M032_GAIN_ANALOG_MASK);
  510. return mt9m032_write(client, MT9M032_GAIN_ALL, reg_val);
  511. }
  512. static int mt9m032_try_ctrl(struct v4l2_ctrl *ctrl)
  513. {
  514. if (ctrl->id == V4L2_CID_GAIN && ctrl->val >= 63) {
  515. /* round because of multiplier used for values >= 63 */
  516. ctrl->val &= ~1;
  517. }
  518. return 0;
  519. }
  520. static int mt9m032_set_ctrl(struct v4l2_ctrl *ctrl)
  521. {
  522. struct mt9m032 *sensor =
  523. container_of(ctrl->handler, struct mt9m032, ctrls);
  524. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  525. int ret;
  526. switch (ctrl->id) {
  527. case V4L2_CID_GAIN:
  528. return mt9m032_set_gain(sensor, ctrl->val);
  529. case V4L2_CID_HFLIP:
  530. /* case V4L2_CID_VFLIP: -- In the same cluster */
  531. return update_read_mode2(sensor, sensor->vflip->val,
  532. sensor->hflip->val);
  533. case V4L2_CID_EXPOSURE:
  534. ret = mt9m032_write(client, MT9M032_SHUTTER_WIDTH_HIGH,
  535. (ctrl->val >> 16) & 0xffff);
  536. if (ret < 0)
  537. return ret;
  538. return mt9m032_write(client, MT9M032_SHUTTER_WIDTH_LOW,
  539. ctrl->val & 0xffff);
  540. }
  541. return 0;
  542. }
  543. static struct v4l2_ctrl_ops mt9m032_ctrl_ops = {
  544. .s_ctrl = mt9m032_set_ctrl,
  545. .try_ctrl = mt9m032_try_ctrl,
  546. };
  547. /* -------------------------------------------------------------------------- */
  548. static const struct v4l2_subdev_core_ops mt9m032_core_ops = {
  549. #ifdef CONFIG_VIDEO_ADV_DEBUG
  550. .g_register = mt9m032_g_register,
  551. .s_register = mt9m032_s_register,
  552. #endif
  553. };
  554. static const struct v4l2_subdev_video_ops mt9m032_video_ops = {
  555. .s_stream = mt9m032_s_stream,
  556. .g_frame_interval = mt9m032_get_frame_interval,
  557. .s_frame_interval = mt9m032_set_frame_interval,
  558. };
  559. static const struct v4l2_subdev_pad_ops mt9m032_pad_ops = {
  560. .enum_mbus_code = mt9m032_enum_mbus_code,
  561. .enum_frame_size = mt9m032_enum_frame_size,
  562. .get_fmt = mt9m032_get_pad_format,
  563. .set_fmt = mt9m032_set_pad_format,
  564. .set_crop = mt9m032_set_pad_crop,
  565. .get_crop = mt9m032_get_pad_crop,
  566. };
  567. static const struct v4l2_subdev_ops mt9m032_ops = {
  568. .core = &mt9m032_core_ops,
  569. .video = &mt9m032_video_ops,
  570. .pad = &mt9m032_pad_ops,
  571. };
  572. /* -----------------------------------------------------------------------------
  573. * Driver initialization and probing
  574. */
  575. static int mt9m032_probe(struct i2c_client *client,
  576. const struct i2c_device_id *devid)
  577. {
  578. struct i2c_adapter *adapter = client->adapter;
  579. struct mt9m032 *sensor;
  580. int chip_version;
  581. int ret;
  582. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  583. dev_warn(&client->dev,
  584. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  585. return -EIO;
  586. }
  587. if (!client->dev.platform_data)
  588. return -ENODEV;
  589. sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
  590. if (sensor == NULL)
  591. return -ENOMEM;
  592. mutex_init(&sensor->lock);
  593. sensor->pdata = client->dev.platform_data;
  594. v4l2_i2c_subdev_init(&sensor->subdev, client, &mt9m032_ops);
  595. sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  596. chip_version = mt9m032_read(client, MT9M032_CHIP_VERSION);
  597. if (chip_version != MT9M032_CHIP_VERSION_VALUE) {
  598. dev_err(&client->dev, "MT9M032 not detected, wrong version "
  599. "0x%04x\n", chip_version);
  600. ret = -ENODEV;
  601. goto error_sensor;
  602. }
  603. dev_info(&client->dev, "MT9M032 detected at address 0x%02x\n",
  604. client->addr);
  605. sensor->frame_interval.numerator = 1;
  606. sensor->frame_interval.denominator = 30;
  607. sensor->crop.left = MT9M032_COLUMN_START_DEF;
  608. sensor->crop.top = MT9M032_ROW_START_DEF;
  609. sensor->crop.width = MT9M032_COLUMN_SIZE_DEF;
  610. sensor->crop.height = MT9M032_ROW_SIZE_DEF;
  611. sensor->format.width = sensor->crop.width;
  612. sensor->format.height = sensor->crop.height;
  613. sensor->format.code = V4L2_MBUS_FMT_Y8_1X8;
  614. sensor->format.field = V4L2_FIELD_NONE;
  615. sensor->format.colorspace = V4L2_COLORSPACE_SRGB;
  616. v4l2_ctrl_handler_init(&sensor->ctrls, 4);
  617. v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
  618. V4L2_CID_GAIN, 0, 127, 1, 64);
  619. sensor->hflip = v4l2_ctrl_new_std(&sensor->ctrls,
  620. &mt9m032_ctrl_ops,
  621. V4L2_CID_HFLIP, 0, 1, 1, 0);
  622. sensor->vflip = v4l2_ctrl_new_std(&sensor->ctrls,
  623. &mt9m032_ctrl_ops,
  624. V4L2_CID_VFLIP, 0, 1, 1, 0);
  625. v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
  626. V4L2_CID_EXPOSURE, MT9M032_SHUTTER_WIDTH_MIN,
  627. MT9M032_SHUTTER_WIDTH_MAX, 1,
  628. MT9M032_SHUTTER_WIDTH_DEF);
  629. if (sensor->ctrls.error) {
  630. ret = sensor->ctrls.error;
  631. dev_err(&client->dev, "control initialization error %d\n", ret);
  632. goto error_ctrl;
  633. }
  634. v4l2_ctrl_cluster(2, &sensor->hflip);
  635. sensor->subdev.ctrl_handler = &sensor->ctrls;
  636. sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
  637. ret = media_entity_init(&sensor->subdev.entity, 1, &sensor->pad, 0);
  638. if (ret < 0)
  639. goto error_ctrl;
  640. ret = mt9m032_write(client, MT9M032_RESET, 1); /* reset on */
  641. if (ret < 0)
  642. goto error_entity;
  643. mt9m032_write(client, MT9M032_RESET, 0); /* reset off */
  644. if (ret < 0)
  645. goto error_entity;
  646. ret = mt9m032_setup_pll(sensor);
  647. if (ret < 0)
  648. goto error_entity;
  649. usleep_range(10000, 11000);
  650. ret = v4l2_ctrl_handler_setup(&sensor->ctrls);
  651. if (ret < 0)
  652. goto error_entity;
  653. /* SIZE */
  654. ret = mt9m032_update_geom_timing(sensor);
  655. if (ret < 0)
  656. goto error_entity;
  657. ret = mt9m032_write(client, 0x41, 0x0000); /* reserved !!! */
  658. if (ret < 0)
  659. goto error_entity;
  660. ret = mt9m032_write(client, 0x42, 0x0003); /* reserved !!! */
  661. if (ret < 0)
  662. goto error_entity;
  663. ret = mt9m032_write(client, 0x43, 0x0003); /* reserved !!! */
  664. if (ret < 0)
  665. goto error_entity;
  666. ret = mt9m032_write(client, 0x7f, 0x0000); /* reserved !!! */
  667. if (ret < 0)
  668. goto error_entity;
  669. if (sensor->pdata->invert_pixclock) {
  670. ret = mt9m032_write(client, MT9M032_PIX_CLK_CTRL,
  671. MT9M032_PIX_CLK_CTRL_INV_PIXCLK);
  672. if (ret < 0)
  673. goto error_entity;
  674. }
  675. ret = mt9m032_write(client, MT9M032_RESTART, 1); /* Restart on */
  676. if (ret < 0)
  677. goto error_entity;
  678. msleep(100);
  679. ret = mt9m032_write(client, MT9M032_RESTART, 0); /* Restart off */
  680. if (ret < 0)
  681. goto error_entity;
  682. msleep(100);
  683. ret = update_formatter2(sensor, false);
  684. if (ret < 0)
  685. goto error_entity;
  686. return ret;
  687. error_entity:
  688. media_entity_cleanup(&sensor->subdev.entity);
  689. error_ctrl:
  690. v4l2_ctrl_handler_free(&sensor->ctrls);
  691. error_sensor:
  692. mutex_destroy(&sensor->lock);
  693. kfree(sensor);
  694. return ret;
  695. }
  696. static int mt9m032_remove(struct i2c_client *client)
  697. {
  698. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  699. struct mt9m032 *sensor = to_mt9m032(subdev);
  700. v4l2_device_unregister_subdev(&sensor->subdev);
  701. v4l2_ctrl_handler_free(&sensor->ctrls);
  702. media_entity_cleanup(&sensor->subdev.entity);
  703. mutex_destroy(&sensor->lock);
  704. kfree(sensor);
  705. return 0;
  706. }
  707. static const struct i2c_device_id mt9m032_id_table[] = {
  708. { MT9M032_NAME, 0 },
  709. { }
  710. };
  711. MODULE_DEVICE_TABLE(i2c, mt9m032_id_table);
  712. static struct i2c_driver mt9m032_i2c_driver = {
  713. .driver = {
  714. .name = MT9M032_NAME,
  715. },
  716. .probe = mt9m032_probe,
  717. .remove = mt9m032_remove,
  718. .id_table = mt9m032_id_table,
  719. };
  720. module_i2c_driver(mt9m032_i2c_driver);
  721. MODULE_AUTHOR("Martin Hostettler <martin@neutronstar.dyndns.org>");
  722. MODULE_DESCRIPTION("MT9M032 camera sensor driver");
  723. MODULE_LICENSE("GPL v2");