mt9t001.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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_pad_config *cfg,
  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(&mt9t001->subdev, cfg, 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_pad_config *cfg,
  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(&mt9t001->subdev, cfg, 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_pad_config *cfg,
  283. struct v4l2_subdev_mbus_code_enum *code)
  284. {
  285. if (code->index > 0)
  286. return -EINVAL;
  287. code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  288. return 0;
  289. }
  290. static int mt9t001_enum_frame_size(struct v4l2_subdev *subdev,
  291. struct v4l2_subdev_pad_config *cfg,
  292. struct v4l2_subdev_frame_size_enum *fse)
  293. {
  294. if (fse->index >= 8 || fse->code != MEDIA_BUS_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_pad_config *cfg,
  304. struct v4l2_subdev_format *format)
  305. {
  306. struct mt9t001 *mt9t001 = to_mt9t001(subdev);
  307. format->format = *__mt9t001_get_pad_format(mt9t001, cfg, format->pad,
  308. format->which);
  309. return 0;
  310. }
  311. static int mt9t001_set_format(struct v4l2_subdev *subdev,
  312. struct v4l2_subdev_pad_config *cfg,
  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, cfg, 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, cfg, 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_selection(struct v4l2_subdev *subdev,
  343. struct v4l2_subdev_pad_config *cfg,
  344. struct v4l2_subdev_selection *sel)
  345. {
  346. struct mt9t001 *mt9t001 = to_mt9t001(subdev);
  347. if (sel->target != V4L2_SEL_TGT_CROP)
  348. return -EINVAL;
  349. sel->r = *__mt9t001_get_pad_crop(mt9t001, cfg, sel->pad, sel->which);
  350. return 0;
  351. }
  352. static int mt9t001_set_selection(struct v4l2_subdev *subdev,
  353. struct v4l2_subdev_pad_config *cfg,
  354. struct v4l2_subdev_selection *sel)
  355. {
  356. struct mt9t001 *mt9t001 = to_mt9t001(subdev);
  357. struct v4l2_mbus_framefmt *__format;
  358. struct v4l2_rect *__crop;
  359. struct v4l2_rect rect;
  360. if (sel->target != V4L2_SEL_TGT_CROP)
  361. return -EINVAL;
  362. /* Clamp the crop rectangle boundaries and align them to a multiple of 2
  363. * pixels.
  364. */
  365. rect.left = clamp(ALIGN(sel->r.left, 2),
  366. MT9T001_COLUMN_START_MIN,
  367. MT9T001_COLUMN_START_MAX);
  368. rect.top = clamp(ALIGN(sel->r.top, 2),
  369. MT9T001_ROW_START_MIN,
  370. MT9T001_ROW_START_MAX);
  371. rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
  372. MT9T001_WINDOW_WIDTH_MIN + 1,
  373. MT9T001_WINDOW_WIDTH_MAX + 1);
  374. rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
  375. MT9T001_WINDOW_HEIGHT_MIN + 1,
  376. MT9T001_WINDOW_HEIGHT_MAX + 1);
  377. rect.width = min_t(unsigned int, rect.width,
  378. MT9T001_PIXEL_ARRAY_WIDTH - rect.left);
  379. rect.height = min_t(unsigned int, rect.height,
  380. MT9T001_PIXEL_ARRAY_HEIGHT - rect.top);
  381. __crop = __mt9t001_get_pad_crop(mt9t001, cfg, sel->pad, sel->which);
  382. if (rect.width != __crop->width || rect.height != __crop->height) {
  383. /* Reset the output image size if the crop rectangle size has
  384. * been modified.
  385. */
  386. __format = __mt9t001_get_pad_format(mt9t001, cfg, sel->pad,
  387. sel->which);
  388. __format->width = rect.width;
  389. __format->height = rect.height;
  390. }
  391. *__crop = rect;
  392. sel->r = rect;
  393. return 0;
  394. }
  395. /* -----------------------------------------------------------------------------
  396. * V4L2 subdev control operations
  397. */
  398. #define V4L2_CID_TEST_PATTERN_COLOR (V4L2_CID_USER_BASE | 0x1001)
  399. #define V4L2_CID_BLACK_LEVEL_AUTO (V4L2_CID_USER_BASE | 0x1002)
  400. #define V4L2_CID_BLACK_LEVEL_OFFSET (V4L2_CID_USER_BASE | 0x1003)
  401. #define V4L2_CID_BLACK_LEVEL_CALIBRATE (V4L2_CID_USER_BASE | 0x1004)
  402. #define V4L2_CID_GAIN_RED (V4L2_CTRL_CLASS_CAMERA | 0x1001)
  403. #define V4L2_CID_GAIN_GREEN_RED (V4L2_CTRL_CLASS_CAMERA | 0x1002)
  404. #define V4L2_CID_GAIN_GREEN_BLUE (V4L2_CTRL_CLASS_CAMERA | 0x1003)
  405. #define V4L2_CID_GAIN_BLUE (V4L2_CTRL_CLASS_CAMERA | 0x1004)
  406. static u16 mt9t001_gain_value(s32 *gain)
  407. {
  408. /* Gain is controlled by 2 analog stages and a digital stage. Valid
  409. * values for the 3 stages are
  410. *
  411. * Stage Min Max Step
  412. * ------------------------------------------
  413. * First analog stage x1 x2 1
  414. * Second analog stage x1 x4 0.125
  415. * Digital stage x1 x16 0.125
  416. *
  417. * To minimize noise, the gain stages should be used in the second
  418. * analog stage, first analog stage, digital stage order. Gain from a
  419. * previous stage should be pushed to its maximum value before the next
  420. * stage is used.
  421. */
  422. if (*gain <= 32)
  423. return *gain;
  424. if (*gain <= 64) {
  425. *gain &= ~1;
  426. return (1 << 6) | (*gain >> 1);
  427. }
  428. *gain &= ~7;
  429. return ((*gain - 64) << 5) | (1 << 6) | 32;
  430. }
  431. static int mt9t001_ctrl_freeze(struct mt9t001 *mt9t001, bool freeze)
  432. {
  433. return mt9t001_set_output_control(mt9t001,
  434. freeze ? 0 : MT9T001_OUTPUT_CONTROL_SYNC,
  435. freeze ? MT9T001_OUTPUT_CONTROL_SYNC : 0);
  436. }
  437. static int mt9t001_s_ctrl(struct v4l2_ctrl *ctrl)
  438. {
  439. static const u8 gains[4] = {
  440. MT9T001_RED_GAIN, MT9T001_GREEN1_GAIN,
  441. MT9T001_GREEN2_GAIN, MT9T001_BLUE_GAIN
  442. };
  443. struct mt9t001 *mt9t001 =
  444. container_of(ctrl->handler, struct mt9t001, ctrls);
  445. struct i2c_client *client = v4l2_get_subdevdata(&mt9t001->subdev);
  446. unsigned int count;
  447. unsigned int i;
  448. u16 value;
  449. int ret;
  450. switch (ctrl->id) {
  451. case V4L2_CID_GAIN_RED:
  452. case V4L2_CID_GAIN_GREEN_RED:
  453. case V4L2_CID_GAIN_GREEN_BLUE:
  454. case V4L2_CID_GAIN_BLUE:
  455. /* Disable control updates if more than one control has changed
  456. * in the cluster.
  457. */
  458. for (i = 0, count = 0; i < 4; ++i) {
  459. struct v4l2_ctrl *gain = mt9t001->gains[i];
  460. if (gain->val != gain->cur.val)
  461. count++;
  462. }
  463. if (count > 1) {
  464. ret = mt9t001_ctrl_freeze(mt9t001, true);
  465. if (ret < 0)
  466. return ret;
  467. }
  468. /* Update the gain controls. */
  469. for (i = 0; i < 4; ++i) {
  470. struct v4l2_ctrl *gain = mt9t001->gains[i];
  471. if (gain->val == gain->cur.val)
  472. continue;
  473. value = mt9t001_gain_value(&gain->val);
  474. ret = mt9t001_write(client, gains[i], value);
  475. if (ret < 0) {
  476. mt9t001_ctrl_freeze(mt9t001, false);
  477. return ret;
  478. }
  479. }
  480. /* Enable control updates. */
  481. if (count > 1) {
  482. ret = mt9t001_ctrl_freeze(mt9t001, false);
  483. if (ret < 0)
  484. return ret;
  485. }
  486. break;
  487. case V4L2_CID_EXPOSURE:
  488. ret = mt9t001_write(client, MT9T001_SHUTTER_WIDTH_LOW,
  489. ctrl->val & 0xffff);
  490. if (ret < 0)
  491. return ret;
  492. return mt9t001_write(client, MT9T001_SHUTTER_WIDTH_HIGH,
  493. ctrl->val >> 16);
  494. case V4L2_CID_TEST_PATTERN:
  495. return mt9t001_set_output_control(mt9t001,
  496. ctrl->val ? 0 : MT9T001_OUTPUT_CONTROL_TEST_DATA,
  497. ctrl->val ? MT9T001_OUTPUT_CONTROL_TEST_DATA : 0);
  498. case V4L2_CID_TEST_PATTERN_COLOR:
  499. return mt9t001_write(client, MT9T001_TEST_DATA, ctrl->val << 2);
  500. case V4L2_CID_BLACK_LEVEL_AUTO:
  501. value = ctrl->val ? 0 : MT9T001_BLACK_LEVEL_OVERRIDE;
  502. ret = mt9t001_write(client, MT9T001_BLACK_LEVEL_CALIBRATION,
  503. value);
  504. if (ret < 0)
  505. return ret;
  506. mt9t001->black_level = value;
  507. break;
  508. case V4L2_CID_BLACK_LEVEL_OFFSET:
  509. ret = mt9t001_write(client, MT9T001_GREEN1_OFFSET, ctrl->val);
  510. if (ret < 0)
  511. return ret;
  512. ret = mt9t001_write(client, MT9T001_GREEN2_OFFSET, ctrl->val);
  513. if (ret < 0)
  514. return ret;
  515. ret = mt9t001_write(client, MT9T001_RED_OFFSET, ctrl->val);
  516. if (ret < 0)
  517. return ret;
  518. return mt9t001_write(client, MT9T001_BLUE_OFFSET, ctrl->val);
  519. case V4L2_CID_BLACK_LEVEL_CALIBRATE:
  520. return mt9t001_write(client, MT9T001_BLACK_LEVEL_CALIBRATION,
  521. MT9T001_BLACK_LEVEL_RECALCULATE |
  522. mt9t001->black_level);
  523. }
  524. return 0;
  525. }
  526. static struct v4l2_ctrl_ops mt9t001_ctrl_ops = {
  527. .s_ctrl = mt9t001_s_ctrl,
  528. };
  529. static const char * const mt9t001_test_pattern_menu[] = {
  530. "Disabled",
  531. "Enabled",
  532. };
  533. static const struct v4l2_ctrl_config mt9t001_ctrls[] = {
  534. {
  535. .ops = &mt9t001_ctrl_ops,
  536. .id = V4L2_CID_TEST_PATTERN_COLOR,
  537. .type = V4L2_CTRL_TYPE_INTEGER,
  538. .name = "Test Pattern Color",
  539. .min = 0,
  540. .max = 1023,
  541. .step = 1,
  542. .def = 0,
  543. .flags = 0,
  544. }, {
  545. .ops = &mt9t001_ctrl_ops,
  546. .id = V4L2_CID_BLACK_LEVEL_AUTO,
  547. .type = V4L2_CTRL_TYPE_BOOLEAN,
  548. .name = "Black Level, Auto",
  549. .min = 0,
  550. .max = 1,
  551. .step = 1,
  552. .def = 1,
  553. .flags = 0,
  554. }, {
  555. .ops = &mt9t001_ctrl_ops,
  556. .id = V4L2_CID_BLACK_LEVEL_OFFSET,
  557. .type = V4L2_CTRL_TYPE_INTEGER,
  558. .name = "Black Level, Offset",
  559. .min = -256,
  560. .max = 255,
  561. .step = 1,
  562. .def = 32,
  563. .flags = 0,
  564. }, {
  565. .ops = &mt9t001_ctrl_ops,
  566. .id = V4L2_CID_BLACK_LEVEL_CALIBRATE,
  567. .type = V4L2_CTRL_TYPE_BUTTON,
  568. .name = "Black Level, Calibrate",
  569. .min = 0,
  570. .max = 0,
  571. .step = 0,
  572. .def = 0,
  573. .flags = V4L2_CTRL_FLAG_WRITE_ONLY,
  574. },
  575. };
  576. static const struct v4l2_ctrl_config mt9t001_gains[] = {
  577. {
  578. .ops = &mt9t001_ctrl_ops,
  579. .id = V4L2_CID_GAIN_RED,
  580. .type = V4L2_CTRL_TYPE_INTEGER,
  581. .name = "Gain, Red",
  582. .min = MT9T001_GLOBAL_GAIN_MIN,
  583. .max = MT9T001_GLOBAL_GAIN_MAX,
  584. .step = 1,
  585. .def = MT9T001_GLOBAL_GAIN_MIN,
  586. .flags = 0,
  587. }, {
  588. .ops = &mt9t001_ctrl_ops,
  589. .id = V4L2_CID_GAIN_GREEN_RED,
  590. .type = V4L2_CTRL_TYPE_INTEGER,
  591. .name = "Gain, Green (R)",
  592. .min = MT9T001_GLOBAL_GAIN_MIN,
  593. .max = MT9T001_GLOBAL_GAIN_MAX,
  594. .step = 1,
  595. .def = MT9T001_GLOBAL_GAIN_MIN,
  596. .flags = 0,
  597. }, {
  598. .ops = &mt9t001_ctrl_ops,
  599. .id = V4L2_CID_GAIN_GREEN_BLUE,
  600. .type = V4L2_CTRL_TYPE_INTEGER,
  601. .name = "Gain, Green (B)",
  602. .min = MT9T001_GLOBAL_GAIN_MIN,
  603. .max = MT9T001_GLOBAL_GAIN_MAX,
  604. .step = 1,
  605. .def = MT9T001_GLOBAL_GAIN_MIN,
  606. .flags = 0,
  607. }, {
  608. .ops = &mt9t001_ctrl_ops,
  609. .id = V4L2_CID_GAIN_BLUE,
  610. .type = V4L2_CTRL_TYPE_INTEGER,
  611. .name = "Gain, Blue",
  612. .min = MT9T001_GLOBAL_GAIN_MIN,
  613. .max = MT9T001_GLOBAL_GAIN_MAX,
  614. .step = 1,
  615. .def = MT9T001_GLOBAL_GAIN_MIN,
  616. .flags = 0,
  617. },
  618. };
  619. /* -----------------------------------------------------------------------------
  620. * V4L2 subdev core operations
  621. */
  622. static int mt9t001_set_power(struct v4l2_subdev *subdev, int on)
  623. {
  624. struct mt9t001 *mt9t001 = to_mt9t001(subdev);
  625. int ret = 0;
  626. mutex_lock(&mt9t001->power_lock);
  627. /* If the power count is modified from 0 to != 0 or from != 0 to 0,
  628. * update the power state.
  629. */
  630. if (mt9t001->power_count == !on) {
  631. ret = __mt9t001_set_power(mt9t001, !!on);
  632. if (ret < 0)
  633. goto out;
  634. }
  635. /* Update the power count. */
  636. mt9t001->power_count += on ? 1 : -1;
  637. WARN_ON(mt9t001->power_count < 0);
  638. out:
  639. mutex_unlock(&mt9t001->power_lock);
  640. return ret;
  641. }
  642. /* -----------------------------------------------------------------------------
  643. * V4L2 subdev internal operations
  644. */
  645. static int mt9t001_registered(struct v4l2_subdev *subdev)
  646. {
  647. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  648. struct mt9t001 *mt9t001 = to_mt9t001(subdev);
  649. s32 data;
  650. int ret;
  651. ret = mt9t001_power_on(mt9t001);
  652. if (ret < 0) {
  653. dev_err(&client->dev, "MT9T001 power up failed\n");
  654. return ret;
  655. }
  656. /* Read out the chip version register */
  657. data = mt9t001_read(client, MT9T001_CHIP_VERSION);
  658. mt9t001_power_off(mt9t001);
  659. if (data != MT9T001_CHIP_ID) {
  660. dev_err(&client->dev,
  661. "MT9T001 not detected, wrong version 0x%04x\n", data);
  662. return -ENODEV;
  663. }
  664. dev_info(&client->dev, "MT9T001 detected at address 0x%02x\n",
  665. client->addr);
  666. return 0;
  667. }
  668. static int mt9t001_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  669. {
  670. struct v4l2_mbus_framefmt *format;
  671. struct v4l2_rect *crop;
  672. crop = v4l2_subdev_get_try_crop(subdev, fh->pad, 0);
  673. crop->left = MT9T001_COLUMN_START_DEF;
  674. crop->top = MT9T001_ROW_START_DEF;
  675. crop->width = MT9T001_WINDOW_WIDTH_DEF + 1;
  676. crop->height = MT9T001_WINDOW_HEIGHT_DEF + 1;
  677. format = v4l2_subdev_get_try_format(subdev, fh->pad, 0);
  678. format->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  679. format->width = MT9T001_WINDOW_WIDTH_DEF + 1;
  680. format->height = MT9T001_WINDOW_HEIGHT_DEF + 1;
  681. format->field = V4L2_FIELD_NONE;
  682. format->colorspace = V4L2_COLORSPACE_SRGB;
  683. return mt9t001_set_power(subdev, 1);
  684. }
  685. static int mt9t001_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  686. {
  687. return mt9t001_set_power(subdev, 0);
  688. }
  689. static struct v4l2_subdev_core_ops mt9t001_subdev_core_ops = {
  690. .s_power = mt9t001_set_power,
  691. };
  692. static struct v4l2_subdev_video_ops mt9t001_subdev_video_ops = {
  693. .s_stream = mt9t001_s_stream,
  694. };
  695. static struct v4l2_subdev_pad_ops mt9t001_subdev_pad_ops = {
  696. .enum_mbus_code = mt9t001_enum_mbus_code,
  697. .enum_frame_size = mt9t001_enum_frame_size,
  698. .get_fmt = mt9t001_get_format,
  699. .set_fmt = mt9t001_set_format,
  700. .get_selection = mt9t001_get_selection,
  701. .set_selection = mt9t001_set_selection,
  702. };
  703. static struct v4l2_subdev_ops mt9t001_subdev_ops = {
  704. .core = &mt9t001_subdev_core_ops,
  705. .video = &mt9t001_subdev_video_ops,
  706. .pad = &mt9t001_subdev_pad_ops,
  707. };
  708. static struct v4l2_subdev_internal_ops mt9t001_subdev_internal_ops = {
  709. .registered = mt9t001_registered,
  710. .open = mt9t001_open,
  711. .close = mt9t001_close,
  712. };
  713. static int mt9t001_probe(struct i2c_client *client,
  714. const struct i2c_device_id *did)
  715. {
  716. struct mt9t001_platform_data *pdata = client->dev.platform_data;
  717. struct mt9t001 *mt9t001;
  718. unsigned int i;
  719. int ret;
  720. if (pdata == NULL) {
  721. dev_err(&client->dev, "No platform data\n");
  722. return -EINVAL;
  723. }
  724. if (!i2c_check_functionality(client->adapter,
  725. I2C_FUNC_SMBUS_WORD_DATA)) {
  726. dev_warn(&client->adapter->dev,
  727. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  728. return -EIO;
  729. }
  730. mt9t001 = devm_kzalloc(&client->dev, sizeof(*mt9t001), GFP_KERNEL);
  731. if (!mt9t001)
  732. return -ENOMEM;
  733. mutex_init(&mt9t001->power_lock);
  734. mt9t001->output_control = MT9T001_OUTPUT_CONTROL_DEF;
  735. mt9t001->regulators[0].supply = "vdd";
  736. mt9t001->regulators[1].supply = "vaa";
  737. ret = devm_regulator_bulk_get(&client->dev, 2, mt9t001->regulators);
  738. if (ret < 0) {
  739. dev_err(&client->dev, "Unable to get regulators\n");
  740. return ret;
  741. }
  742. mt9t001->clk = devm_clk_get(&client->dev, NULL);
  743. if (IS_ERR(mt9t001->clk)) {
  744. dev_err(&client->dev, "Unable to get clock\n");
  745. return PTR_ERR(mt9t001->clk);
  746. }
  747. v4l2_ctrl_handler_init(&mt9t001->ctrls, ARRAY_SIZE(mt9t001_ctrls) +
  748. ARRAY_SIZE(mt9t001_gains) + 4);
  749. v4l2_ctrl_new_std(&mt9t001->ctrls, &mt9t001_ctrl_ops,
  750. V4L2_CID_EXPOSURE, MT9T001_SHUTTER_WIDTH_MIN,
  751. MT9T001_SHUTTER_WIDTH_MAX, 1,
  752. MT9T001_SHUTTER_WIDTH_DEF);
  753. v4l2_ctrl_new_std(&mt9t001->ctrls, &mt9t001_ctrl_ops,
  754. V4L2_CID_BLACK_LEVEL, 1, 1, 1, 1);
  755. v4l2_ctrl_new_std(&mt9t001->ctrls, &mt9t001_ctrl_ops,
  756. V4L2_CID_PIXEL_RATE, pdata->ext_clk, pdata->ext_clk,
  757. 1, pdata->ext_clk);
  758. v4l2_ctrl_new_std_menu_items(&mt9t001->ctrls, &mt9t001_ctrl_ops,
  759. V4L2_CID_TEST_PATTERN,
  760. ARRAY_SIZE(mt9t001_test_pattern_menu) - 1, 0,
  761. 0, mt9t001_test_pattern_menu);
  762. for (i = 0; i < ARRAY_SIZE(mt9t001_ctrls); ++i)
  763. v4l2_ctrl_new_custom(&mt9t001->ctrls, &mt9t001_ctrls[i], NULL);
  764. for (i = 0; i < ARRAY_SIZE(mt9t001_gains); ++i)
  765. mt9t001->gains[i] = v4l2_ctrl_new_custom(&mt9t001->ctrls,
  766. &mt9t001_gains[i], NULL);
  767. v4l2_ctrl_cluster(ARRAY_SIZE(mt9t001_gains), mt9t001->gains);
  768. mt9t001->subdev.ctrl_handler = &mt9t001->ctrls;
  769. if (mt9t001->ctrls.error) {
  770. printk(KERN_INFO "%s: control initialization error %d\n",
  771. __func__, mt9t001->ctrls.error);
  772. ret = -EINVAL;
  773. goto done;
  774. }
  775. mt9t001->crop.left = MT9T001_COLUMN_START_DEF;
  776. mt9t001->crop.top = MT9T001_ROW_START_DEF;
  777. mt9t001->crop.width = MT9T001_WINDOW_WIDTH_DEF + 1;
  778. mt9t001->crop.height = MT9T001_WINDOW_HEIGHT_DEF + 1;
  779. mt9t001->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
  780. mt9t001->format.width = MT9T001_WINDOW_WIDTH_DEF + 1;
  781. mt9t001->format.height = MT9T001_WINDOW_HEIGHT_DEF + 1;
  782. mt9t001->format.field = V4L2_FIELD_NONE;
  783. mt9t001->format.colorspace = V4L2_COLORSPACE_SRGB;
  784. v4l2_i2c_subdev_init(&mt9t001->subdev, client, &mt9t001_subdev_ops);
  785. mt9t001->subdev.internal_ops = &mt9t001_subdev_internal_ops;
  786. mt9t001->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  787. mt9t001->pad.flags = MEDIA_PAD_FL_SOURCE;
  788. ret = media_entity_init(&mt9t001->subdev.entity, 1, &mt9t001->pad, 0);
  789. done:
  790. if (ret < 0) {
  791. v4l2_ctrl_handler_free(&mt9t001->ctrls);
  792. media_entity_cleanup(&mt9t001->subdev.entity);
  793. }
  794. return ret;
  795. }
  796. static int mt9t001_remove(struct i2c_client *client)
  797. {
  798. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  799. struct mt9t001 *mt9t001 = to_mt9t001(subdev);
  800. v4l2_ctrl_handler_free(&mt9t001->ctrls);
  801. v4l2_device_unregister_subdev(subdev);
  802. media_entity_cleanup(&subdev->entity);
  803. return 0;
  804. }
  805. static const struct i2c_device_id mt9t001_id[] = {
  806. { "mt9t001", 0 },
  807. { }
  808. };
  809. MODULE_DEVICE_TABLE(i2c, mt9t001_id);
  810. static struct i2c_driver mt9t001_driver = {
  811. .driver = {
  812. .name = "mt9t001",
  813. },
  814. .probe = mt9t001_probe,
  815. .remove = mt9t001_remove,
  816. .id_table = mt9t001_id,
  817. };
  818. module_i2c_driver(mt9t001_driver);
  819. MODULE_DESCRIPTION("Aptina (Micron) MT9T001 Camera driver");
  820. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  821. MODULE_LICENSE("GPL");