mt9m032.c 25 KB

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