mt9t001.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /*
  2. * Driver for MT9T001 CMOS Image Sensor from Aptina (Micron)
  3. *
  4. * Copyright (C) 2010-2011, Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  5. *
  6. * Based on the MT9M001 driver,
  7. *
  8. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/i2c.h>
  16. #include <linux/log2.h>
  17. #include <linux/module.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/slab.h>
  20. #include <linux/videodev2.h>
  21. #include <linux/v4l2-mediabus.h>
  22. #include <media/mt9t001.h>
  23. #include <media/v4l2-ctrls.h>
  24. #include <media/v4l2-device.h>
  25. #include <media/v4l2-subdev.h>
  26. #define MT9T001_PIXEL_ARRAY_HEIGHT 1568
  27. #define MT9T001_PIXEL_ARRAY_WIDTH 2112
  28. #define MT9T001_CHIP_VERSION 0x00
  29. #define MT9T001_CHIP_ID 0x1621
  30. #define MT9T001_ROW_START 0x01
  31. #define MT9T001_ROW_START_MIN 0
  32. #define MT9T001_ROW_START_DEF 20
  33. #define MT9T001_ROW_START_MAX 1534
  34. #define MT9T001_COLUMN_START 0x02
  35. #define MT9T001_COLUMN_START_MIN 0
  36. #define MT9T001_COLUMN_START_DEF 32
  37. #define MT9T001_COLUMN_START_MAX 2046
  38. #define MT9T001_WINDOW_HEIGHT 0x03
  39. #define MT9T001_WINDOW_HEIGHT_MIN 1
  40. #define MT9T001_WINDOW_HEIGHT_DEF 1535
  41. #define MT9T001_WINDOW_HEIGHT_MAX 1567
  42. #define MT9T001_WINDOW_WIDTH 0x04
  43. #define MT9T001_WINDOW_WIDTH_MIN 1
  44. #define MT9T001_WINDOW_WIDTH_DEF 2047
  45. #define MT9T001_WINDOW_WIDTH_MAX 2111
  46. #define MT9T001_HORIZONTAL_BLANKING 0x05
  47. #define MT9T001_HORIZONTAL_BLANKING_MIN 21
  48. #define MT9T001_HORIZONTAL_BLANKING_MAX 1023
  49. #define MT9T001_VERTICAL_BLANKING 0x06
  50. #define MT9T001_VERTICAL_BLANKING_MIN 3
  51. #define MT9T001_VERTICAL_BLANKING_MAX 1023
  52. #define MT9T001_OUTPUT_CONTROL 0x07
  53. #define MT9T001_OUTPUT_CONTROL_SYNC (1 << 0)
  54. #define MT9T001_OUTPUT_CONTROL_CHIP_ENABLE (1 << 1)
  55. #define MT9T001_OUTPUT_CONTROL_TEST_DATA (1 << 6)
  56. #define MT9T001_OUTPUT_CONTROL_DEF 0x0002
  57. #define MT9T001_SHUTTER_WIDTH_HIGH 0x08
  58. #define MT9T001_SHUTTER_WIDTH_LOW 0x09
  59. #define MT9T001_SHUTTER_WIDTH_MIN 1
  60. #define MT9T001_SHUTTER_WIDTH_DEF 1561
  61. #define MT9T001_SHUTTER_WIDTH_MAX (1024 * 1024)
  62. #define MT9T001_PIXEL_CLOCK 0x0a
  63. #define MT9T001_PIXEL_CLOCK_INVERT (1 << 15)
  64. #define MT9T001_PIXEL_CLOCK_SHIFT_MASK (7 << 8)
  65. #define MT9T001_PIXEL_CLOCK_SHIFT_SHIFT 8
  66. #define MT9T001_PIXEL_CLOCK_DIVIDE_MASK (0x7f << 0)
  67. #define MT9T001_FRAME_RESTART 0x0b
  68. #define MT9T001_SHUTTER_DELAY 0x0c
  69. #define MT9T001_SHUTTER_DELAY_MAX 2047
  70. #define MT9T001_RESET 0x0d
  71. #define MT9T001_READ_MODE1 0x1e
  72. #define MT9T001_READ_MODE_SNAPSHOT (1 << 8)
  73. #define MT9T001_READ_MODE_STROBE_ENABLE (1 << 9)
  74. #define MT9T001_READ_MODE_STROBE_WIDTH (1 << 10)
  75. #define MT9T001_READ_MODE_STROBE_OVERRIDE (1 << 11)
  76. #define MT9T001_READ_MODE2 0x20
  77. #define MT9T001_READ_MODE_BAD_FRAMES (1 << 0)
  78. #define MT9T001_READ_MODE_LINE_VALID_CONTINUOUS (1 << 9)
  79. #define MT9T001_READ_MODE_LINE_VALID_FRAME (1 << 10)
  80. #define MT9T001_READ_MODE3 0x21
  81. #define MT9T001_READ_MODE_GLOBAL_RESET (1 << 0)
  82. #define MT9T001_READ_MODE_GHST_CTL (1 << 1)
  83. #define MT9T001_ROW_ADDRESS_MODE 0x22
  84. #define MT9T001_ROW_SKIP_MASK (7 << 0)
  85. #define MT9T001_ROW_BIN_MASK (3 << 3)
  86. #define MT9T001_ROW_BIN_SHIFT 3
  87. #define MT9T001_COLUMN_ADDRESS_MODE 0x23
  88. #define MT9T001_COLUMN_SKIP_MASK (7 << 0)
  89. #define MT9T001_COLUMN_BIN_MASK (3 << 3)
  90. #define MT9T001_COLUMN_BIN_SHIFT 3
  91. #define MT9T001_GREEN1_GAIN 0x2b
  92. #define MT9T001_BLUE_GAIN 0x2c
  93. #define MT9T001_RED_GAIN 0x2d
  94. #define MT9T001_GREEN2_GAIN 0x2e
  95. #define MT9T001_TEST_DATA 0x32
  96. #define MT9T001_GLOBAL_GAIN 0x35
  97. #define MT9T001_GLOBAL_GAIN_MIN 8
  98. #define MT9T001_GLOBAL_GAIN_MAX 1024
  99. #define MT9T001_BLACK_LEVEL 0x49
  100. #define MT9T001_ROW_BLACK_DEFAULT_OFFSET 0x4b
  101. #define MT9T001_BLC_DELTA_THRESHOLDS 0x5d
  102. #define MT9T001_CAL_THRESHOLDS 0x5f
  103. #define MT9T001_GREEN1_OFFSET 0x60
  104. #define MT9T001_GREEN2_OFFSET 0x61
  105. #define MT9T001_BLACK_LEVEL_CALIBRATION 0x62
  106. #define MT9T001_BLACK_LEVEL_OVERRIDE (1 << 0)
  107. #define MT9T001_BLACK_LEVEL_DISABLE_OFFSET (1 << 1)
  108. #define MT9T001_BLACK_LEVEL_RECALCULATE (1 << 12)
  109. #define MT9T001_BLACK_LEVEL_LOCK_RED_BLUE (1 << 13)
  110. #define MT9T001_BLACK_LEVEL_LOCK_GREEN (1 << 14)
  111. #define MT9T001_RED_OFFSET 0x63
  112. #define MT9T001_BLUE_OFFSET 0x64
  113. struct mt9t001 {
  114. struct v4l2_subdev subdev;
  115. struct media_pad pad;
  116. struct clk *clk;
  117. struct regulator_bulk_data regulators[2];
  118. struct mutex power_lock; /* lock to protect power_count */
  119. int power_count;
  120. struct v4l2_mbus_framefmt format;
  121. struct v4l2_rect crop;
  122. struct v4l2_ctrl_handler ctrls;
  123. struct v4l2_ctrl *gains[4];
  124. u16 output_control;
  125. u16 black_level;
  126. };
  127. static inline struct mt9t001 *to_mt9t001(struct v4l2_subdev *sd)
  128. {
  129. return container_of(sd, struct mt9t001, subdev);
  130. }
  131. static int mt9t001_read(struct i2c_client *client, u8 reg)
  132. {
  133. return i2c_smbus_read_word_swapped(client, reg);
  134. }
  135. static int mt9t001_write(struct i2c_client *client, u8 reg, u16 data)
  136. {
  137. return i2c_smbus_write_word_swapped(client, reg, data);
  138. }
  139. static int mt9t001_set_output_control(struct mt9t001 *mt9t001, u16 clear,
  140. u16 set)
  141. {
  142. struct i2c_client *client = v4l2_get_subdevdata(&mt9t001->subdev);
  143. u16 value = (mt9t001->output_control & ~clear) | set;
  144. int ret;
  145. if (value == mt9t001->output_control)
  146. return 0;
  147. ret = mt9t001_write(client, MT9T001_OUTPUT_CONTROL, value);
  148. if (ret < 0)
  149. return ret;
  150. mt9t001->output_control = value;
  151. return 0;
  152. }
  153. static int mt9t001_reset(struct mt9t001 *mt9t001)
  154. {
  155. struct i2c_client *client = v4l2_get_subdevdata(&mt9t001->subdev);
  156. int ret;
  157. /* Reset the chip and stop data read out */
  158. ret = mt9t001_write(client, MT9T001_RESET, 1);
  159. if (ret < 0)
  160. return ret;
  161. ret = mt9t001_write(client, MT9T001_RESET, 0);
  162. if (ret < 0)
  163. return ret;
  164. mt9t001->output_control = MT9T001_OUTPUT_CONTROL_DEF;
  165. return mt9t001_set_output_control(mt9t001,
  166. MT9T001_OUTPUT_CONTROL_CHIP_ENABLE,
  167. 0);
  168. }
  169. static int mt9t001_power_on(struct mt9t001 *mt9t001)
  170. {
  171. int ret;
  172. /* Bring up the supplies */
  173. ret = regulator_bulk_enable(ARRAY_SIZE(mt9t001->regulators),
  174. mt9t001->regulators);
  175. if (ret < 0)
  176. return ret;
  177. /* Enable clock */
  178. ret = clk_prepare_enable(mt9t001->clk);
  179. if (ret < 0)
  180. regulator_bulk_disable(ARRAY_SIZE(mt9t001->regulators),
  181. mt9t001->regulators);
  182. return ret;
  183. }
  184. static void mt9t001_power_off(struct mt9t001 *mt9t001)
  185. {
  186. regulator_bulk_disable(ARRAY_SIZE(mt9t001->regulators),
  187. mt9t001->regulators);
  188. clk_disable_unprepare(mt9t001->clk);
  189. }
  190. static int __mt9t001_set_power(struct mt9t001 *mt9t001, bool on)
  191. {
  192. struct i2c_client *client = v4l2_get_subdevdata(&mt9t001->subdev);
  193. int ret;
  194. if (!on) {
  195. mt9t001_power_off(mt9t001);
  196. return 0;
  197. }
  198. ret = mt9t001_power_on(mt9t001);
  199. if (ret < 0)
  200. return ret;
  201. ret = mt9t001_reset(mt9t001);
  202. if (ret < 0) {
  203. dev_err(&client->dev, "Failed to reset the camera\n");
  204. return ret;
  205. }
  206. return v4l2_ctrl_handler_setup(&mt9t001->ctrls);
  207. }
  208. /* -----------------------------------------------------------------------------
  209. * V4L2 subdev video operations
  210. */
  211. static struct v4l2_mbus_framefmt *
  212. __mt9t001_get_pad_format(struct mt9t001 *mt9t001, struct v4l2_subdev_fh *fh,
  213. unsigned int pad, enum v4l2_subdev_format_whence which)
  214. {
  215. switch (which) {
  216. case V4L2_SUBDEV_FORMAT_TRY:
  217. return v4l2_subdev_get_try_format(fh, pad);
  218. case V4L2_SUBDEV_FORMAT_ACTIVE:
  219. return &mt9t001->format;
  220. default:
  221. return NULL;
  222. }
  223. }
  224. static struct v4l2_rect *
  225. __mt9t001_get_pad_crop(struct mt9t001 *mt9t001, struct v4l2_subdev_fh *fh,
  226. unsigned int pad, enum v4l2_subdev_format_whence which)
  227. {
  228. switch (which) {
  229. case V4L2_SUBDEV_FORMAT_TRY:
  230. return v4l2_subdev_get_try_crop(fh, pad);
  231. case V4L2_SUBDEV_FORMAT_ACTIVE:
  232. return &mt9t001->crop;
  233. default:
  234. return NULL;
  235. }
  236. }
  237. static int mt9t001_s_stream(struct v4l2_subdev *subdev, int enable)
  238. {
  239. const u16 mode = MT9T001_OUTPUT_CONTROL_CHIP_ENABLE;
  240. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  241. struct mt9t001_platform_data *pdata = client->dev.platform_data;
  242. struct mt9t001 *mt9t001 = to_mt9t001(subdev);
  243. struct v4l2_mbus_framefmt *format = &mt9t001->format;
  244. struct v4l2_rect *crop = &mt9t001->crop;
  245. unsigned int hratio;
  246. unsigned int vratio;
  247. int ret;
  248. if (!enable)
  249. return mt9t001_set_output_control(mt9t001, mode, 0);
  250. /* Configure the pixel clock polarity */
  251. if (pdata->clk_pol) {
  252. ret = mt9t001_write(client, MT9T001_PIXEL_CLOCK,
  253. MT9T001_PIXEL_CLOCK_INVERT);
  254. if (ret < 0)
  255. return ret;
  256. }
  257. /* Configure the window size and row/column bin */
  258. hratio = DIV_ROUND_CLOSEST(crop->width, format->width);
  259. vratio = DIV_ROUND_CLOSEST(crop->height, format->height);
  260. ret = mt9t001_write(client, MT9T001_ROW_ADDRESS_MODE, hratio - 1);
  261. if (ret < 0)
  262. return ret;
  263. ret = mt9t001_write(client, MT9T001_COLUMN_ADDRESS_MODE, vratio - 1);
  264. if (ret < 0)
  265. return ret;
  266. ret = mt9t001_write(client, MT9T001_COLUMN_START, crop->left);
  267. if (ret < 0)
  268. return ret;
  269. ret = mt9t001_write(client, MT9T001_ROW_START, crop->top);
  270. if (ret < 0)
  271. return ret;
  272. ret = mt9t001_write(client, MT9T001_WINDOW_WIDTH, crop->width - 1);
  273. if (ret < 0)
  274. return ret;
  275. ret = mt9t001_write(client, MT9T001_WINDOW_HEIGHT, crop->height - 1);
  276. if (ret < 0)
  277. return ret;
  278. /* Switch to master "normal" mode */
  279. return mt9t001_set_output_control(mt9t001, 0, mode);
  280. }
  281. static int mt9t001_enum_mbus_code(struct v4l2_subdev *subdev,
  282. struct v4l2_subdev_fh *fh,
  283. struct v4l2_subdev_mbus_code_enum *code)
  284. {
  285. if (code->index > 0)
  286. return -EINVAL;
  287. code->code = V4L2_MBUS_FMT_SGRBG10_1X10;
  288. return 0;
  289. }
  290. static int mt9t001_enum_frame_size(struct v4l2_subdev *subdev,
  291. struct v4l2_subdev_fh *fh,
  292. struct v4l2_subdev_frame_size_enum *fse)
  293. {
  294. if (fse->index >= 8 || fse->code != V4L2_MBUS_FMT_SGRBG10_1X10)
  295. return -EINVAL;
  296. fse->min_width = (MT9T001_WINDOW_WIDTH_DEF + 1) / fse->index;
  297. fse->max_width = fse->min_width;
  298. fse->min_height = (MT9T001_WINDOW_HEIGHT_DEF + 1) / fse->index;
  299. fse->max_height = fse->min_height;
  300. return 0;
  301. }
  302. static int mt9t001_get_format(struct v4l2_subdev *subdev,
  303. struct v4l2_subdev_fh *fh,
  304. struct v4l2_subdev_format *format)
  305. {
  306. struct mt9t001 *mt9t001 = to_mt9t001(subdev);
  307. format->format = *__mt9t001_get_pad_format(mt9t001, fh, format->pad,
  308. format->which);
  309. return 0;
  310. }
  311. static int mt9t001_set_format(struct v4l2_subdev *subdev,
  312. struct v4l2_subdev_fh *fh,
  313. struct v4l2_subdev_format *format)
  314. {
  315. struct mt9t001 *mt9t001 = to_mt9t001(subdev);
  316. struct v4l2_mbus_framefmt *__format;
  317. struct v4l2_rect *__crop;
  318. unsigned int width;
  319. unsigned int height;
  320. unsigned int hratio;
  321. unsigned int vratio;
  322. __crop = __mt9t001_get_pad_crop(mt9t001, fh, format->pad,
  323. format->which);
  324. /* Clamp the width and height to avoid dividing by zero. */
  325. width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
  326. max_t(unsigned int, __crop->width / 8,
  327. MT9T001_WINDOW_HEIGHT_MIN + 1),
  328. __crop->width);
  329. height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
  330. max_t(unsigned int, __crop->height / 8,
  331. MT9T001_WINDOW_HEIGHT_MIN + 1),
  332. __crop->height);
  333. hratio = DIV_ROUND_CLOSEST(__crop->width, width);
  334. vratio = DIV_ROUND_CLOSEST(__crop->height, height);
  335. __format = __mt9t001_get_pad_format(mt9t001, fh, format->pad,
  336. format->which);
  337. __format->width = __crop->width / hratio;
  338. __format->height = __crop->height / vratio;
  339. format->format = *__format;
  340. return 0;
  341. }
  342. static int mt9t001_get_crop(struct v4l2_subdev *subdev,
  343. struct v4l2_subdev_fh *fh,
  344. struct v4l2_subdev_crop *crop)
  345. {
  346. struct mt9t001 *mt9t001 = to_mt9t001(subdev);
  347. crop->rect = *__mt9t001_get_pad_crop(mt9t001, fh, crop->pad,
  348. crop->which);
  349. return 0;
  350. }
  351. static int mt9t001_set_crop(struct v4l2_subdev *subdev,
  352. struct v4l2_subdev_fh *fh,
  353. struct v4l2_subdev_crop *crop)
  354. {
  355. struct mt9t001 *mt9t001 = to_mt9t001(subdev);
  356. struct v4l2_mbus_framefmt *__format;
  357. struct v4l2_rect *__crop;
  358. struct v4l2_rect rect;
  359. /* Clamp the crop rectangle boundaries and align them to a multiple of 2
  360. * pixels.
  361. */
  362. rect.left = clamp(ALIGN(crop->rect.left, 2),
  363. MT9T001_COLUMN_START_MIN,
  364. MT9T001_COLUMN_START_MAX);
  365. rect.top = clamp(ALIGN(crop->rect.top, 2),
  366. MT9T001_ROW_START_MIN,
  367. MT9T001_ROW_START_MAX);
  368. rect.width = clamp_t(unsigned int, ALIGN(crop->rect.width, 2),
  369. MT9T001_WINDOW_WIDTH_MIN + 1,
  370. MT9T001_WINDOW_WIDTH_MAX + 1);
  371. rect.height = clamp_t(unsigned int, ALIGN(crop->rect.height, 2),
  372. MT9T001_WINDOW_HEIGHT_MIN + 1,
  373. MT9T001_WINDOW_HEIGHT_MAX + 1);
  374. rect.width = min_t(unsigned int, rect.width,
  375. MT9T001_PIXEL_ARRAY_WIDTH - rect.left);
  376. rect.height = min_t(unsigned int, rect.height,
  377. MT9T001_PIXEL_ARRAY_HEIGHT - rect.top);
  378. __crop = __mt9t001_get_pad_crop(mt9t001, fh, crop->pad, crop->which);
  379. if (rect.width != __crop->width || rect.height != __crop->height) {
  380. /* Reset the output image size if the crop rectangle size has
  381. * been modified.
  382. */
  383. __format = __mt9t001_get_pad_format(mt9t001, fh, crop->pad,
  384. crop->which);
  385. __format->width = rect.width;
  386. __format->height = rect.height;
  387. }
  388. *__crop = rect;
  389. crop->rect = rect;
  390. return 0;
  391. }
  392. /* -----------------------------------------------------------------------------
  393. * V4L2 subdev control operations
  394. */
  395. #define V4L2_CID_TEST_PATTERN_COLOR (V4L2_CID_USER_BASE | 0x1001)
  396. #define V4L2_CID_BLACK_LEVEL_AUTO (V4L2_CID_USER_BASE | 0x1002)
  397. #define V4L2_CID_BLACK_LEVEL_OFFSET (V4L2_CID_USER_BASE | 0x1003)
  398. #define V4L2_CID_BLACK_LEVEL_CALIBRATE (V4L2_CID_USER_BASE | 0x1004)
  399. #define V4L2_CID_GAIN_RED (V4L2_CTRL_CLASS_CAMERA | 0x1001)
  400. #define V4L2_CID_GAIN_GREEN_RED (V4L2_CTRL_CLASS_CAMERA | 0x1002)
  401. #define V4L2_CID_GAIN_GREEN_BLUE (V4L2_CTRL_CLASS_CAMERA | 0x1003)
  402. #define V4L2_CID_GAIN_BLUE (V4L2_CTRL_CLASS_CAMERA | 0x1004)
  403. static u16 mt9t001_gain_value(s32 *gain)
  404. {
  405. /* Gain is controlled by 2 analog stages and a digital stage. Valid
  406. * values for the 3 stages are
  407. *
  408. * Stage Min Max Step
  409. * ------------------------------------------
  410. * First analog stage x1 x2 1
  411. * Second analog stage x1 x4 0.125
  412. * Digital stage x1 x16 0.125
  413. *
  414. * To minimize noise, the gain stages should be used in the second
  415. * analog stage, first analog stage, digital stage order. Gain from a
  416. * previous stage should be pushed to its maximum value before the next
  417. * stage is used.
  418. */
  419. if (*gain <= 32)
  420. return *gain;
  421. if (*gain <= 64) {
  422. *gain &= ~1;
  423. return (1 << 6) | (*gain >> 1);
  424. }
  425. *gain &= ~7;
  426. return ((*gain - 64) << 5) | (1 << 6) | 32;
  427. }
  428. static int mt9t001_ctrl_freeze(struct mt9t001 *mt9t001, bool freeze)
  429. {
  430. return mt9t001_set_output_control(mt9t001,
  431. freeze ? 0 : MT9T001_OUTPUT_CONTROL_SYNC,
  432. freeze ? MT9T001_OUTPUT_CONTROL_SYNC : 0);
  433. }
  434. static int mt9t001_s_ctrl(struct v4l2_ctrl *ctrl)
  435. {
  436. static const u8 gains[4] = {
  437. MT9T001_RED_GAIN, MT9T001_GREEN1_GAIN,
  438. MT9T001_GREEN2_GAIN, MT9T001_BLUE_GAIN
  439. };
  440. struct mt9t001 *mt9t001 =
  441. container_of(ctrl->handler, struct mt9t001, ctrls);
  442. struct i2c_client *client = v4l2_get_subdevdata(&mt9t001->subdev);
  443. unsigned int count;
  444. unsigned int i;
  445. u16 value;
  446. int ret;
  447. switch (ctrl->id) {
  448. case V4L2_CID_GAIN_RED:
  449. case V4L2_CID_GAIN_GREEN_RED:
  450. case V4L2_CID_GAIN_GREEN_BLUE:
  451. case V4L2_CID_GAIN_BLUE:
  452. /* Disable control updates if more than one control has changed
  453. * in the cluster.
  454. */
  455. for (i = 0, count = 0; i < 4; ++i) {
  456. struct v4l2_ctrl *gain = mt9t001->gains[i];
  457. if (gain->val != gain->cur.val)
  458. count++;
  459. }
  460. if (count > 1) {
  461. ret = mt9t001_ctrl_freeze(mt9t001, true);
  462. if (ret < 0)
  463. return ret;
  464. }
  465. /* Update the gain controls. */
  466. for (i = 0; i < 4; ++i) {
  467. struct v4l2_ctrl *gain = mt9t001->gains[i];
  468. if (gain->val == gain->cur.val)
  469. continue;
  470. value = mt9t001_gain_value(&gain->val);
  471. ret = mt9t001_write(client, gains[i], value);
  472. if (ret < 0) {
  473. mt9t001_ctrl_freeze(mt9t001, false);
  474. return ret;
  475. }
  476. }
  477. /* Enable control updates. */
  478. if (count > 1) {
  479. ret = mt9t001_ctrl_freeze(mt9t001, false);
  480. if (ret < 0)
  481. return ret;
  482. }
  483. break;
  484. case V4L2_CID_EXPOSURE:
  485. ret = mt9t001_write(client, MT9T001_SHUTTER_WIDTH_LOW,
  486. ctrl->val & 0xffff);
  487. if (ret < 0)
  488. return ret;
  489. return mt9t001_write(client, MT9T001_SHUTTER_WIDTH_HIGH,
  490. ctrl->val >> 16);
  491. case V4L2_CID_TEST_PATTERN:
  492. return mt9t001_set_output_control(mt9t001,
  493. ctrl->val ? 0 : MT9T001_OUTPUT_CONTROL_TEST_DATA,
  494. ctrl->val ? MT9T001_OUTPUT_CONTROL_TEST_DATA : 0);
  495. case V4L2_CID_TEST_PATTERN_COLOR:
  496. return mt9t001_write(client, MT9T001_TEST_DATA, ctrl->val << 2);
  497. case V4L2_CID_BLACK_LEVEL_AUTO:
  498. value = ctrl->val ? 0 : MT9T001_BLACK_LEVEL_OVERRIDE;
  499. ret = mt9t001_write(client, MT9T001_BLACK_LEVEL_CALIBRATION,
  500. value);
  501. if (ret < 0)
  502. return ret;
  503. mt9t001->black_level = value;
  504. break;
  505. case V4L2_CID_BLACK_LEVEL_OFFSET:
  506. ret = mt9t001_write(client, MT9T001_GREEN1_OFFSET, ctrl->val);
  507. if (ret < 0)
  508. return ret;
  509. ret = mt9t001_write(client, MT9T001_GREEN2_OFFSET, ctrl->val);
  510. if (ret < 0)
  511. return ret;
  512. ret = mt9t001_write(client, MT9T001_RED_OFFSET, ctrl->val);
  513. if (ret < 0)
  514. return ret;
  515. return mt9t001_write(client, MT9T001_BLUE_OFFSET, ctrl->val);
  516. case V4L2_CID_BLACK_LEVEL_CALIBRATE:
  517. return mt9t001_write(client, MT9T001_BLACK_LEVEL_CALIBRATION,
  518. MT9T001_BLACK_LEVEL_RECALCULATE |
  519. mt9t001->black_level);
  520. }
  521. return 0;
  522. }
  523. static struct v4l2_ctrl_ops mt9t001_ctrl_ops = {
  524. .s_ctrl = mt9t001_s_ctrl,
  525. };
  526. static const char * const mt9t001_test_pattern_menu[] = {
  527. "Disabled",
  528. "Enabled",
  529. };
  530. static const struct v4l2_ctrl_config mt9t001_ctrls[] = {
  531. {
  532. .ops = &mt9t001_ctrl_ops,
  533. .id = V4L2_CID_TEST_PATTERN_COLOR,
  534. .type = V4L2_CTRL_TYPE_INTEGER,
  535. .name = "Test Pattern Color",
  536. .min = 0,
  537. .max = 1023,
  538. .step = 1,
  539. .def = 0,
  540. .flags = 0,
  541. }, {
  542. .ops = &mt9t001_ctrl_ops,
  543. .id = V4L2_CID_BLACK_LEVEL_AUTO,
  544. .type = V4L2_CTRL_TYPE_BOOLEAN,
  545. .name = "Black Level, Auto",
  546. .min = 0,
  547. .max = 1,
  548. .step = 1,
  549. .def = 1,
  550. .flags = 0,
  551. }, {
  552. .ops = &mt9t001_ctrl_ops,
  553. .id = V4L2_CID_BLACK_LEVEL_OFFSET,
  554. .type = V4L2_CTRL_TYPE_INTEGER,
  555. .name = "Black Level, Offset",
  556. .min = -256,
  557. .max = 255,
  558. .step = 1,
  559. .def = 32,
  560. .flags = 0,
  561. }, {
  562. .ops = &mt9t001_ctrl_ops,
  563. .id = V4L2_CID_BLACK_LEVEL_CALIBRATE,
  564. .type = V4L2_CTRL_TYPE_BUTTON,
  565. .name = "Black Level, Calibrate",
  566. .min = 0,
  567. .max = 0,
  568. .step = 0,
  569. .def = 0,
  570. .flags = V4L2_CTRL_FLAG_WRITE_ONLY,
  571. },
  572. };
  573. static const struct v4l2_ctrl_config mt9t001_gains[] = {
  574. {
  575. .ops = &mt9t001_ctrl_ops,
  576. .id = V4L2_CID_GAIN_RED,
  577. .type = V4L2_CTRL_TYPE_INTEGER,
  578. .name = "Gain, Red",
  579. .min = MT9T001_GLOBAL_GAIN_MIN,
  580. .max = MT9T001_GLOBAL_GAIN_MAX,
  581. .step = 1,
  582. .def = MT9T001_GLOBAL_GAIN_MIN,
  583. .flags = 0,
  584. }, {
  585. .ops = &mt9t001_ctrl_ops,
  586. .id = V4L2_CID_GAIN_GREEN_RED,
  587. .type = V4L2_CTRL_TYPE_INTEGER,
  588. .name = "Gain, Green (R)",
  589. .min = MT9T001_GLOBAL_GAIN_MIN,
  590. .max = MT9T001_GLOBAL_GAIN_MAX,
  591. .step = 1,
  592. .def = MT9T001_GLOBAL_GAIN_MIN,
  593. .flags = 0,
  594. }, {
  595. .ops = &mt9t001_ctrl_ops,
  596. .id = V4L2_CID_GAIN_GREEN_BLUE,
  597. .type = V4L2_CTRL_TYPE_INTEGER,
  598. .name = "Gain, Green (B)",
  599. .min = MT9T001_GLOBAL_GAIN_MIN,
  600. .max = MT9T001_GLOBAL_GAIN_MAX,
  601. .step = 1,
  602. .def = MT9T001_GLOBAL_GAIN_MIN,
  603. .flags = 0,
  604. }, {
  605. .ops = &mt9t001_ctrl_ops,
  606. .id = V4L2_CID_GAIN_BLUE,
  607. .type = V4L2_CTRL_TYPE_INTEGER,
  608. .name = "Gain, Blue",
  609. .min = MT9T001_GLOBAL_GAIN_MIN,
  610. .max = MT9T001_GLOBAL_GAIN_MAX,
  611. .step = 1,
  612. .def = MT9T001_GLOBAL_GAIN_MIN,
  613. .flags = 0,
  614. },
  615. };
  616. /* -----------------------------------------------------------------------------
  617. * V4L2 subdev core operations
  618. */
  619. static int mt9t001_set_power(struct v4l2_subdev *subdev, int on)
  620. {
  621. struct mt9t001 *mt9t001 = to_mt9t001(subdev);
  622. int ret = 0;
  623. mutex_lock(&mt9t001->power_lock);
  624. /* If the power count is modified from 0 to != 0 or from != 0 to 0,
  625. * update the power state.
  626. */
  627. if (mt9t001->power_count == !on) {
  628. ret = __mt9t001_set_power(mt9t001, !!on);
  629. if (ret < 0)
  630. goto out;
  631. }
  632. /* Update the power count. */
  633. mt9t001->power_count += on ? 1 : -1;
  634. WARN_ON(mt9t001->power_count < 0);
  635. out:
  636. mutex_unlock(&mt9t001->power_lock);
  637. return ret;
  638. }
  639. /* -----------------------------------------------------------------------------
  640. * V4L2 subdev internal operations
  641. */
  642. static int mt9t001_registered(struct v4l2_subdev *subdev)
  643. {
  644. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  645. struct mt9t001 *mt9t001 = to_mt9t001(subdev);
  646. s32 data;
  647. int ret;
  648. ret = mt9t001_power_on(mt9t001);
  649. if (ret < 0) {
  650. dev_err(&client->dev, "MT9T001 power up failed\n");
  651. return ret;
  652. }
  653. /* Read out the chip version register */
  654. data = mt9t001_read(client, MT9T001_CHIP_VERSION);
  655. mt9t001_power_off(mt9t001);
  656. if (data != MT9T001_CHIP_ID) {
  657. dev_err(&client->dev,
  658. "MT9T001 not detected, wrong version 0x%04x\n", data);
  659. return -ENODEV;
  660. }
  661. dev_info(&client->dev, "MT9T001 detected at address 0x%02x\n",
  662. client->addr);
  663. return 0;
  664. }
  665. static int mt9t001_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  666. {
  667. struct v4l2_mbus_framefmt *format;
  668. struct v4l2_rect *crop;
  669. crop = v4l2_subdev_get_try_crop(fh, 0);
  670. crop->left = MT9T001_COLUMN_START_DEF;
  671. crop->top = MT9T001_ROW_START_DEF;
  672. crop->width = MT9T001_WINDOW_WIDTH_DEF + 1;
  673. crop->height = MT9T001_WINDOW_HEIGHT_DEF + 1;
  674. format = v4l2_subdev_get_try_format(fh, 0);
  675. format->code = V4L2_MBUS_FMT_SGRBG10_1X10;
  676. format->width = MT9T001_WINDOW_WIDTH_DEF + 1;
  677. format->height = MT9T001_WINDOW_HEIGHT_DEF + 1;
  678. format->field = V4L2_FIELD_NONE;
  679. format->colorspace = V4L2_COLORSPACE_SRGB;
  680. return mt9t001_set_power(subdev, 1);
  681. }
  682. static int mt9t001_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  683. {
  684. return mt9t001_set_power(subdev, 0);
  685. }
  686. static struct v4l2_subdev_core_ops mt9t001_subdev_core_ops = {
  687. .s_power = mt9t001_set_power,
  688. };
  689. static struct v4l2_subdev_video_ops mt9t001_subdev_video_ops = {
  690. .s_stream = mt9t001_s_stream,
  691. };
  692. static struct v4l2_subdev_pad_ops mt9t001_subdev_pad_ops = {
  693. .enum_mbus_code = mt9t001_enum_mbus_code,
  694. .enum_frame_size = mt9t001_enum_frame_size,
  695. .get_fmt = mt9t001_get_format,
  696. .set_fmt = mt9t001_set_format,
  697. .get_crop = mt9t001_get_crop,
  698. .set_crop = mt9t001_set_crop,
  699. };
  700. static struct v4l2_subdev_ops mt9t001_subdev_ops = {
  701. .core = &mt9t001_subdev_core_ops,
  702. .video = &mt9t001_subdev_video_ops,
  703. .pad = &mt9t001_subdev_pad_ops,
  704. };
  705. static struct v4l2_subdev_internal_ops mt9t001_subdev_internal_ops = {
  706. .registered = mt9t001_registered,
  707. .open = mt9t001_open,
  708. .close = mt9t001_close,
  709. };
  710. static int mt9t001_probe(struct i2c_client *client,
  711. const struct i2c_device_id *did)
  712. {
  713. struct mt9t001_platform_data *pdata = client->dev.platform_data;
  714. struct mt9t001 *mt9t001;
  715. unsigned int i;
  716. int ret;
  717. if (pdata == NULL) {
  718. dev_err(&client->dev, "No platform data\n");
  719. return -EINVAL;
  720. }
  721. if (!i2c_check_functionality(client->adapter,
  722. I2C_FUNC_SMBUS_WORD_DATA)) {
  723. dev_warn(&client->adapter->dev,
  724. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  725. return -EIO;
  726. }
  727. mt9t001 = devm_kzalloc(&client->dev, sizeof(*mt9t001), GFP_KERNEL);
  728. if (!mt9t001)
  729. return -ENOMEM;
  730. mutex_init(&mt9t001->power_lock);
  731. mt9t001->output_control = MT9T001_OUTPUT_CONTROL_DEF;
  732. mt9t001->regulators[0].supply = "vdd";
  733. mt9t001->regulators[1].supply = "vaa";
  734. ret = devm_regulator_bulk_get(&client->dev, 2, mt9t001->regulators);
  735. if (ret < 0) {
  736. dev_err(&client->dev, "Unable to get regulators\n");
  737. return ret;
  738. }
  739. mt9t001->clk = devm_clk_get(&client->dev, NULL);
  740. if (IS_ERR(mt9t001->clk)) {
  741. dev_err(&client->dev, "Unable to get clock\n");
  742. return PTR_ERR(mt9t001->clk);
  743. }
  744. v4l2_ctrl_handler_init(&mt9t001->ctrls, ARRAY_SIZE(mt9t001_ctrls) +
  745. ARRAY_SIZE(mt9t001_gains) + 4);
  746. v4l2_ctrl_new_std(&mt9t001->ctrls, &mt9t001_ctrl_ops,
  747. V4L2_CID_EXPOSURE, MT9T001_SHUTTER_WIDTH_MIN,
  748. MT9T001_SHUTTER_WIDTH_MAX, 1,
  749. MT9T001_SHUTTER_WIDTH_DEF);
  750. v4l2_ctrl_new_std(&mt9t001->ctrls, &mt9t001_ctrl_ops,
  751. V4L2_CID_BLACK_LEVEL, 1, 1, 1, 1);
  752. v4l2_ctrl_new_std(&mt9t001->ctrls, &mt9t001_ctrl_ops,
  753. V4L2_CID_PIXEL_RATE, pdata->ext_clk, pdata->ext_clk,
  754. 1, pdata->ext_clk);
  755. v4l2_ctrl_new_std_menu_items(&mt9t001->ctrls, &mt9t001_ctrl_ops,
  756. V4L2_CID_TEST_PATTERN,
  757. ARRAY_SIZE(mt9t001_test_pattern_menu) - 1, 0,
  758. 0, mt9t001_test_pattern_menu);
  759. for (i = 0; i < ARRAY_SIZE(mt9t001_ctrls); ++i)
  760. v4l2_ctrl_new_custom(&mt9t001->ctrls, &mt9t001_ctrls[i], NULL);
  761. for (i = 0; i < ARRAY_SIZE(mt9t001_gains); ++i)
  762. mt9t001->gains[i] = v4l2_ctrl_new_custom(&mt9t001->ctrls,
  763. &mt9t001_gains[i], NULL);
  764. v4l2_ctrl_cluster(ARRAY_SIZE(mt9t001_gains), mt9t001->gains);
  765. mt9t001->subdev.ctrl_handler = &mt9t001->ctrls;
  766. if (mt9t001->ctrls.error) {
  767. printk(KERN_INFO "%s: control initialization error %d\n",
  768. __func__, mt9t001->ctrls.error);
  769. ret = -EINVAL;
  770. goto done;
  771. }
  772. mt9t001->crop.left = MT9T001_COLUMN_START_DEF;
  773. mt9t001->crop.top = MT9T001_ROW_START_DEF;
  774. mt9t001->crop.width = MT9T001_WINDOW_WIDTH_DEF + 1;
  775. mt9t001->crop.height = MT9T001_WINDOW_HEIGHT_DEF + 1;
  776. mt9t001->format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
  777. mt9t001->format.width = MT9T001_WINDOW_WIDTH_DEF + 1;
  778. mt9t001->format.height = MT9T001_WINDOW_HEIGHT_DEF + 1;
  779. mt9t001->format.field = V4L2_FIELD_NONE;
  780. mt9t001->format.colorspace = V4L2_COLORSPACE_SRGB;
  781. v4l2_i2c_subdev_init(&mt9t001->subdev, client, &mt9t001_subdev_ops);
  782. mt9t001->subdev.internal_ops = &mt9t001_subdev_internal_ops;
  783. mt9t001->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  784. mt9t001->pad.flags = MEDIA_PAD_FL_SOURCE;
  785. ret = media_entity_init(&mt9t001->subdev.entity, 1, &mt9t001->pad, 0);
  786. done:
  787. if (ret < 0) {
  788. v4l2_ctrl_handler_free(&mt9t001->ctrls);
  789. media_entity_cleanup(&mt9t001->subdev.entity);
  790. }
  791. return ret;
  792. }
  793. static int mt9t001_remove(struct i2c_client *client)
  794. {
  795. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  796. struct mt9t001 *mt9t001 = to_mt9t001(subdev);
  797. v4l2_ctrl_handler_free(&mt9t001->ctrls);
  798. v4l2_device_unregister_subdev(subdev);
  799. media_entity_cleanup(&subdev->entity);
  800. return 0;
  801. }
  802. static const struct i2c_device_id mt9t001_id[] = {
  803. { "mt9t001", 0 },
  804. { }
  805. };
  806. MODULE_DEVICE_TABLE(i2c, mt9t001_id);
  807. static struct i2c_driver mt9t001_driver = {
  808. .driver = {
  809. .name = "mt9t001",
  810. },
  811. .probe = mt9t001_probe,
  812. .remove = mt9t001_remove,
  813. .id_table = mt9t001_id,
  814. };
  815. module_i2c_driver(mt9t001_driver);
  816. MODULE_DESCRIPTION("Aptina (Micron) MT9T001 Camera driver");
  817. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  818. MODULE_LICENSE("GPL");