et8ek8_driver.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. /*
  2. * et8ek8_driver.c
  3. *
  4. * Copyright (C) 2008 Nokia Corporation
  5. *
  6. * Contact: Sakari Ailus <sakari.ailus@iki.fi>
  7. * Tuukka Toivonen <tuukkat76@gmail.com>
  8. * Pavel Machek <pavel@ucw.cz>
  9. *
  10. * Based on code from Toni Leinonen <toni.leinonen@offcode.fi>.
  11. *
  12. * This driver is based on the Micron MT9T012 camera imager driver
  13. * (C) Texas Instruments.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. */
  24. #include <linux/clk.h>
  25. #include <linux/delay.h>
  26. #include <linux/gpio/consumer.h>
  27. #include <linux/i2c.h>
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/mutex.h>
  31. #include <linux/regulator/consumer.h>
  32. #include <linux/slab.h>
  33. #include <linux/sort.h>
  34. #include <linux/v4l2-mediabus.h>
  35. #include <media/media-entity.h>
  36. #include <media/v4l2-ctrls.h>
  37. #include <media/v4l2-device.h>
  38. #include <media/v4l2-subdev.h>
  39. #include "et8ek8_reg.h"
  40. #define ET8EK8_NAME "et8ek8"
  41. #define ET8EK8_PRIV_MEM_SIZE 128
  42. #define ET8EK8_MAX_MSG 48
  43. struct et8ek8_sensor {
  44. struct v4l2_subdev subdev;
  45. struct media_pad pad;
  46. struct v4l2_mbus_framefmt format;
  47. struct gpio_desc *reset;
  48. struct regulator *vana;
  49. struct clk *ext_clk;
  50. u32 xclk_freq;
  51. u16 version;
  52. struct v4l2_ctrl_handler ctrl_handler;
  53. struct v4l2_ctrl *exposure;
  54. struct v4l2_ctrl *pixel_rate;
  55. struct et8ek8_reglist *current_reglist;
  56. u8 priv_mem[ET8EK8_PRIV_MEM_SIZE];
  57. struct mutex power_lock;
  58. int power_count;
  59. };
  60. #define to_et8ek8_sensor(sd) container_of(sd, struct et8ek8_sensor, subdev)
  61. enum et8ek8_versions {
  62. ET8EK8_REV_1 = 0x0001,
  63. ET8EK8_REV_2,
  64. };
  65. /*
  66. * This table describes what should be written to the sensor register
  67. * for each gain value. The gain(index in the table) is in terms of
  68. * 0.1EV, i.e. 10 indexes in the table give 2 time more gain [0] in
  69. * the *analog gain, [1] in the digital gain
  70. *
  71. * Analog gain [dB] = 20*log10(regvalue/32); 0x20..0x100
  72. */
  73. static struct et8ek8_gain {
  74. u16 analog;
  75. u16 digital;
  76. } const et8ek8_gain_table[] = {
  77. { 32, 0}, /* x1 */
  78. { 34, 0},
  79. { 37, 0},
  80. { 39, 0},
  81. { 42, 0},
  82. { 45, 0},
  83. { 49, 0},
  84. { 52, 0},
  85. { 56, 0},
  86. { 60, 0},
  87. { 64, 0}, /* x2 */
  88. { 69, 0},
  89. { 74, 0},
  90. { 79, 0},
  91. { 84, 0},
  92. { 91, 0},
  93. { 97, 0},
  94. {104, 0},
  95. {111, 0},
  96. {119, 0},
  97. {128, 0}, /* x4 */
  98. {137, 0},
  99. {147, 0},
  100. {158, 0},
  101. {169, 0},
  102. {181, 0},
  103. {194, 0},
  104. {208, 0},
  105. {223, 0},
  106. {239, 0},
  107. {256, 0}, /* x8 */
  108. {256, 73},
  109. {256, 152},
  110. {256, 236},
  111. {256, 327},
  112. {256, 424},
  113. {256, 528},
  114. {256, 639},
  115. {256, 758},
  116. {256, 886},
  117. {256, 1023}, /* x16 */
  118. };
  119. /* Register definitions */
  120. #define REG_REVISION_NUMBER_L 0x1200
  121. #define REG_REVISION_NUMBER_H 0x1201
  122. #define PRIV_MEM_START_REG 0x0008
  123. #define PRIV_MEM_WIN_SIZE 8
  124. #define ET8EK8_I2C_DELAY 3 /* msec delay b/w accesses */
  125. #define USE_CRC 1
  126. /*
  127. * Register access helpers
  128. *
  129. * Read a 8/16/32-bit i2c register. The value is returned in 'val'.
  130. * Returns zero if successful, or non-zero otherwise.
  131. */
  132. static int et8ek8_i2c_read_reg(struct i2c_client *client, u16 data_length,
  133. u16 reg, u32 *val)
  134. {
  135. int r;
  136. struct i2c_msg msg;
  137. unsigned char data[4];
  138. if (!client->adapter)
  139. return -ENODEV;
  140. if (data_length != ET8EK8_REG_8BIT && data_length != ET8EK8_REG_16BIT)
  141. return -EINVAL;
  142. msg.addr = client->addr;
  143. msg.flags = 0;
  144. msg.len = 2;
  145. msg.buf = data;
  146. /* high byte goes out first */
  147. data[0] = (u8) (reg >> 8);
  148. data[1] = (u8) (reg & 0xff);
  149. r = i2c_transfer(client->adapter, &msg, 1);
  150. if (r < 0)
  151. goto err;
  152. msg.len = data_length;
  153. msg.flags = I2C_M_RD;
  154. r = i2c_transfer(client->adapter, &msg, 1);
  155. if (r < 0)
  156. goto err;
  157. *val = 0;
  158. /* high byte comes first */
  159. if (data_length == ET8EK8_REG_8BIT)
  160. *val = data[0];
  161. else
  162. *val = (data[1] << 8) + data[0];
  163. return 0;
  164. err:
  165. dev_err(&client->dev, "read from offset 0x%x error %d\n", reg, r);
  166. return r;
  167. }
  168. static void et8ek8_i2c_create_msg(struct i2c_client *client, u16 len, u16 reg,
  169. u32 val, struct i2c_msg *msg,
  170. unsigned char *buf)
  171. {
  172. msg->addr = client->addr;
  173. msg->flags = 0; /* Write */
  174. msg->len = 2 + len;
  175. msg->buf = buf;
  176. /* high byte goes out first */
  177. buf[0] = (u8) (reg >> 8);
  178. buf[1] = (u8) (reg & 0xff);
  179. switch (len) {
  180. case ET8EK8_REG_8BIT:
  181. buf[2] = (u8) (val) & 0xff;
  182. break;
  183. case ET8EK8_REG_16BIT:
  184. buf[2] = (u8) (val) & 0xff;
  185. buf[3] = (u8) (val >> 8) & 0xff;
  186. break;
  187. default:
  188. WARN_ONCE(1, ET8EK8_NAME ": %s: invalid message length.\n",
  189. __func__);
  190. }
  191. }
  192. /*
  193. * A buffered write method that puts the wanted register write
  194. * commands in a message list and passes the list to the i2c framework
  195. */
  196. static int et8ek8_i2c_buffered_write_regs(struct i2c_client *client,
  197. const struct et8ek8_reg *wnext,
  198. int cnt)
  199. {
  200. struct i2c_msg msg[ET8EK8_MAX_MSG];
  201. unsigned char data[ET8EK8_MAX_MSG][6];
  202. int wcnt = 0;
  203. u16 reg, data_length;
  204. u32 val;
  205. if (WARN_ONCE(cnt > ET8EK8_MAX_MSG,
  206. ET8EK8_NAME ": %s: too many messages.\n", __func__)) {
  207. return -EINVAL;
  208. }
  209. /* Create new write messages for all writes */
  210. while (wcnt < cnt) {
  211. data_length = wnext->type;
  212. reg = wnext->reg;
  213. val = wnext->val;
  214. wnext++;
  215. et8ek8_i2c_create_msg(client, data_length, reg,
  216. val, &msg[wcnt], &data[wcnt][0]);
  217. /* Update write count */
  218. wcnt++;
  219. }
  220. /* Now we send everything ... */
  221. return i2c_transfer(client->adapter, msg, wcnt);
  222. }
  223. /*
  224. * Write a list of registers to i2c device.
  225. *
  226. * The list of registers is terminated by ET8EK8_REG_TERM.
  227. * Returns zero if successful, or non-zero otherwise.
  228. */
  229. static int et8ek8_i2c_write_regs(struct i2c_client *client,
  230. const struct et8ek8_reg *regs)
  231. {
  232. int r, cnt = 0;
  233. const struct et8ek8_reg *next;
  234. if (!client->adapter)
  235. return -ENODEV;
  236. if (!regs)
  237. return -EINVAL;
  238. /* Initialize list pointers to the start of the list */
  239. next = regs;
  240. do {
  241. /*
  242. * We have to go through the list to figure out how
  243. * many regular writes we have in a row
  244. */
  245. while (next->type != ET8EK8_REG_TERM &&
  246. next->type != ET8EK8_REG_DELAY) {
  247. /*
  248. * Here we check that the actual length fields
  249. * are valid
  250. */
  251. if (WARN(next->type != ET8EK8_REG_8BIT &&
  252. next->type != ET8EK8_REG_16BIT,
  253. "Invalid type = %d", next->type)) {
  254. return -EINVAL;
  255. }
  256. /*
  257. * Increment count of successive writes and
  258. * read pointer
  259. */
  260. cnt++;
  261. next++;
  262. }
  263. /* Now we start writing ... */
  264. r = et8ek8_i2c_buffered_write_regs(client, regs, cnt);
  265. /* ... and then check that everything was OK */
  266. if (r < 0) {
  267. dev_err(&client->dev, "i2c transfer error!\n");
  268. return r;
  269. }
  270. /*
  271. * If we ran into a sleep statement when going through
  272. * the list, this is where we snooze for the required time
  273. */
  274. if (next->type == ET8EK8_REG_DELAY) {
  275. msleep(next->val);
  276. /*
  277. * ZZZ ...
  278. * Update list pointers and cnt and start over ...
  279. */
  280. next++;
  281. regs = next;
  282. cnt = 0;
  283. }
  284. } while (next->type != ET8EK8_REG_TERM);
  285. return 0;
  286. }
  287. /*
  288. * Write to a 8/16-bit register.
  289. * Returns zero if successful, or non-zero otherwise.
  290. */
  291. static int et8ek8_i2c_write_reg(struct i2c_client *client, u16 data_length,
  292. u16 reg, u32 val)
  293. {
  294. int r;
  295. struct i2c_msg msg;
  296. unsigned char data[6];
  297. if (!client->adapter)
  298. return -ENODEV;
  299. if (data_length != ET8EK8_REG_8BIT && data_length != ET8EK8_REG_16BIT)
  300. return -EINVAL;
  301. et8ek8_i2c_create_msg(client, data_length, reg, val, &msg, data);
  302. r = i2c_transfer(client->adapter, &msg, 1);
  303. if (r < 0) {
  304. dev_err(&client->dev,
  305. "wrote 0x%x to offset 0x%x error %d\n", val, reg, r);
  306. return r;
  307. }
  308. return 0;
  309. }
  310. static struct et8ek8_reglist *et8ek8_reglist_find_type(
  311. struct et8ek8_meta_reglist *meta,
  312. u16 type)
  313. {
  314. struct et8ek8_reglist **next = &meta->reglist[0].ptr;
  315. while (*next) {
  316. if ((*next)->type == type)
  317. return *next;
  318. next++;
  319. }
  320. return NULL;
  321. }
  322. static int et8ek8_i2c_reglist_find_write(struct i2c_client *client,
  323. struct et8ek8_meta_reglist *meta,
  324. u16 type)
  325. {
  326. struct et8ek8_reglist *reglist;
  327. reglist = et8ek8_reglist_find_type(meta, type);
  328. if (!reglist)
  329. return -EINVAL;
  330. return et8ek8_i2c_write_regs(client, reglist->regs);
  331. }
  332. static struct et8ek8_reglist **et8ek8_reglist_first(
  333. struct et8ek8_meta_reglist *meta)
  334. {
  335. return &meta->reglist[0].ptr;
  336. }
  337. static void et8ek8_reglist_to_mbus(const struct et8ek8_reglist *reglist,
  338. struct v4l2_mbus_framefmt *fmt)
  339. {
  340. fmt->width = reglist->mode.window_width;
  341. fmt->height = reglist->mode.window_height;
  342. fmt->code = reglist->mode.bus_format;
  343. }
  344. static struct et8ek8_reglist *et8ek8_reglist_find_mode_fmt(
  345. struct et8ek8_meta_reglist *meta,
  346. struct v4l2_mbus_framefmt *fmt)
  347. {
  348. struct et8ek8_reglist **list = et8ek8_reglist_first(meta);
  349. struct et8ek8_reglist *best_match = NULL;
  350. struct et8ek8_reglist *best_other = NULL;
  351. struct v4l2_mbus_framefmt format;
  352. unsigned int max_dist_match = (unsigned int)-1;
  353. unsigned int max_dist_other = (unsigned int)-1;
  354. /*
  355. * Find the mode with the closest image size. The distance between
  356. * image sizes is the size in pixels of the non-overlapping regions
  357. * between the requested size and the frame-specified size.
  358. *
  359. * Store both the closest mode that matches the requested format, and
  360. * the closest mode for all other formats. The best match is returned
  361. * if found, otherwise the best mode with a non-matching format is
  362. * returned.
  363. */
  364. for (; *list; list++) {
  365. unsigned int dist;
  366. if ((*list)->type != ET8EK8_REGLIST_MODE)
  367. continue;
  368. et8ek8_reglist_to_mbus(*list, &format);
  369. dist = min(fmt->width, format.width)
  370. * min(fmt->height, format.height);
  371. dist = format.width * format.height
  372. + fmt->width * fmt->height - 2 * dist;
  373. if (fmt->code == format.code) {
  374. if (dist < max_dist_match || !best_match) {
  375. best_match = *list;
  376. max_dist_match = dist;
  377. }
  378. } else {
  379. if (dist < max_dist_other || !best_other) {
  380. best_other = *list;
  381. max_dist_other = dist;
  382. }
  383. }
  384. }
  385. return best_match ? best_match : best_other;
  386. }
  387. #define TIMEPERFRAME_AVG_FPS(t) \
  388. (((t).denominator + ((t).numerator >> 1)) / (t).numerator)
  389. static struct et8ek8_reglist *et8ek8_reglist_find_mode_ival(
  390. struct et8ek8_meta_reglist *meta,
  391. struct et8ek8_reglist *current_reglist,
  392. struct v4l2_fract *timeperframe)
  393. {
  394. int fps = TIMEPERFRAME_AVG_FPS(*timeperframe);
  395. struct et8ek8_reglist **list = et8ek8_reglist_first(meta);
  396. struct et8ek8_mode *current_mode = &current_reglist->mode;
  397. for (; *list; list++) {
  398. struct et8ek8_mode *mode = &(*list)->mode;
  399. if ((*list)->type != ET8EK8_REGLIST_MODE)
  400. continue;
  401. if (mode->window_width != current_mode->window_width ||
  402. mode->window_height != current_mode->window_height)
  403. continue;
  404. if (TIMEPERFRAME_AVG_FPS(mode->timeperframe) == fps)
  405. return *list;
  406. }
  407. return NULL;
  408. }
  409. static int et8ek8_reglist_cmp(const void *a, const void *b)
  410. {
  411. const struct et8ek8_reglist **list1 = (const struct et8ek8_reglist **)a,
  412. **list2 = (const struct et8ek8_reglist **)b;
  413. /* Put real modes in the beginning. */
  414. if ((*list1)->type == ET8EK8_REGLIST_MODE &&
  415. (*list2)->type != ET8EK8_REGLIST_MODE)
  416. return -1;
  417. if ((*list1)->type != ET8EK8_REGLIST_MODE &&
  418. (*list2)->type == ET8EK8_REGLIST_MODE)
  419. return 1;
  420. /* Descending width. */
  421. if ((*list1)->mode.window_width > (*list2)->mode.window_width)
  422. return -1;
  423. if ((*list1)->mode.window_width < (*list2)->mode.window_width)
  424. return 1;
  425. if ((*list1)->mode.window_height > (*list2)->mode.window_height)
  426. return -1;
  427. if ((*list1)->mode.window_height < (*list2)->mode.window_height)
  428. return 1;
  429. return 0;
  430. }
  431. static int et8ek8_reglist_import(struct i2c_client *client,
  432. struct et8ek8_meta_reglist *meta)
  433. {
  434. int nlists = 0, i;
  435. dev_info(&client->dev, "meta_reglist version %s\n", meta->version);
  436. while (meta->reglist[nlists].ptr)
  437. nlists++;
  438. if (!nlists)
  439. return -EINVAL;
  440. sort(&meta->reglist[0].ptr, nlists, sizeof(meta->reglist[0].ptr),
  441. et8ek8_reglist_cmp, NULL);
  442. i = nlists;
  443. nlists = 0;
  444. while (i--) {
  445. struct et8ek8_reglist *list;
  446. list = meta->reglist[nlists].ptr;
  447. dev_dbg(&client->dev,
  448. "%s: type %d\tw %d\th %d\tfmt %x\tival %d/%d\tptr %p\n",
  449. __func__,
  450. list->type,
  451. list->mode.window_width, list->mode.window_height,
  452. list->mode.bus_format,
  453. list->mode.timeperframe.numerator,
  454. list->mode.timeperframe.denominator,
  455. (void *)meta->reglist[nlists].ptr);
  456. nlists++;
  457. }
  458. return 0;
  459. }
  460. /* Called to change the V4L2 gain control value. This function
  461. * rounds and clamps the given value and updates the V4L2 control value.
  462. * If power is on, also updates the sensor analog and digital gains.
  463. * gain is in 0.1 EV (exposure value) units.
  464. */
  465. static int et8ek8_set_gain(struct et8ek8_sensor *sensor, s32 gain)
  466. {
  467. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  468. struct et8ek8_gain new;
  469. int r;
  470. new = et8ek8_gain_table[gain];
  471. /* FIXME: optimise I2C writes! */
  472. r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT,
  473. 0x124a, new.analog >> 8);
  474. if (r)
  475. return r;
  476. r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT,
  477. 0x1249, new.analog & 0xff);
  478. if (r)
  479. return r;
  480. r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT,
  481. 0x124d, new.digital >> 8);
  482. if (r)
  483. return r;
  484. r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT,
  485. 0x124c, new.digital & 0xff);
  486. return r;
  487. }
  488. static int et8ek8_set_test_pattern(struct et8ek8_sensor *sensor, s32 mode)
  489. {
  490. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  491. int cbh_mode, cbv_mode, tp_mode, din_sw, r1420, rval;
  492. /* Values for normal mode */
  493. cbh_mode = 0;
  494. cbv_mode = 0;
  495. tp_mode = 0;
  496. din_sw = 0x00;
  497. r1420 = 0xF0;
  498. if (mode) {
  499. /* Test pattern mode */
  500. if (mode < 5) {
  501. cbh_mode = 1;
  502. cbv_mode = 1;
  503. tp_mode = mode + 3;
  504. } else {
  505. cbh_mode = 0;
  506. cbv_mode = 0;
  507. tp_mode = mode - 4 + 3;
  508. }
  509. din_sw = 0x01;
  510. r1420 = 0xE0;
  511. }
  512. rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x111B,
  513. tp_mode << 4);
  514. if (rval)
  515. return rval;
  516. rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1121,
  517. cbh_mode << 7);
  518. if (rval)
  519. return rval;
  520. rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1124,
  521. cbv_mode << 7);
  522. if (rval)
  523. return rval;
  524. rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x112C, din_sw);
  525. if (rval)
  526. return rval;
  527. return et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1420, r1420);
  528. }
  529. /* -----------------------------------------------------------------------------
  530. * V4L2 controls
  531. */
  532. static int et8ek8_set_ctrl(struct v4l2_ctrl *ctrl)
  533. {
  534. struct et8ek8_sensor *sensor =
  535. container_of(ctrl->handler, struct et8ek8_sensor, ctrl_handler);
  536. switch (ctrl->id) {
  537. case V4L2_CID_GAIN:
  538. return et8ek8_set_gain(sensor, ctrl->val);
  539. case V4L2_CID_EXPOSURE:
  540. {
  541. struct i2c_client *client =
  542. v4l2_get_subdevdata(&sensor->subdev);
  543. return et8ek8_i2c_write_reg(client, ET8EK8_REG_16BIT, 0x1243,
  544. ctrl->val);
  545. }
  546. case V4L2_CID_TEST_PATTERN:
  547. return et8ek8_set_test_pattern(sensor, ctrl->val);
  548. case V4L2_CID_PIXEL_RATE:
  549. return 0;
  550. default:
  551. return -EINVAL;
  552. }
  553. }
  554. static const struct v4l2_ctrl_ops et8ek8_ctrl_ops = {
  555. .s_ctrl = et8ek8_set_ctrl,
  556. };
  557. static const char * const et8ek8_test_pattern_menu[] = {
  558. "Normal",
  559. "Vertical colorbar",
  560. "Horizontal colorbar",
  561. "Scale",
  562. "Ramp",
  563. "Small vertical colorbar",
  564. "Small horizontal colorbar",
  565. "Small scale",
  566. "Small ramp",
  567. };
  568. static int et8ek8_init_controls(struct et8ek8_sensor *sensor)
  569. {
  570. s32 max_rows;
  571. v4l2_ctrl_handler_init(&sensor->ctrl_handler, 4);
  572. /* V4L2_CID_GAIN */
  573. v4l2_ctrl_new_std(&sensor->ctrl_handler, &et8ek8_ctrl_ops,
  574. V4L2_CID_GAIN, 0, ARRAY_SIZE(et8ek8_gain_table) - 1,
  575. 1, 0);
  576. max_rows = sensor->current_reglist->mode.max_exp;
  577. {
  578. u32 min = 1, max = max_rows;
  579. sensor->exposure =
  580. v4l2_ctrl_new_std(&sensor->ctrl_handler,
  581. &et8ek8_ctrl_ops, V4L2_CID_EXPOSURE,
  582. min, max, min, max);
  583. }
  584. /* V4L2_CID_PIXEL_RATE */
  585. sensor->pixel_rate =
  586. v4l2_ctrl_new_std(&sensor->ctrl_handler, &et8ek8_ctrl_ops,
  587. V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
  588. /* V4L2_CID_TEST_PATTERN */
  589. v4l2_ctrl_new_std_menu_items(&sensor->ctrl_handler,
  590. &et8ek8_ctrl_ops, V4L2_CID_TEST_PATTERN,
  591. ARRAY_SIZE(et8ek8_test_pattern_menu) - 1,
  592. 0, 0, et8ek8_test_pattern_menu);
  593. if (sensor->ctrl_handler.error)
  594. return sensor->ctrl_handler.error;
  595. sensor->subdev.ctrl_handler = &sensor->ctrl_handler;
  596. return 0;
  597. }
  598. static void et8ek8_update_controls(struct et8ek8_sensor *sensor)
  599. {
  600. struct v4l2_ctrl *ctrl;
  601. struct et8ek8_mode *mode = &sensor->current_reglist->mode;
  602. u32 min, max, pixel_rate;
  603. static const int S = 8;
  604. ctrl = sensor->exposure;
  605. min = 1;
  606. max = mode->max_exp;
  607. /*
  608. * Calculate average pixel clock per line. Assume buffers can spread
  609. * the data over horizontal blanking time. Rounding upwards.
  610. * Formula taken from stock Nokia N900 kernel.
  611. */
  612. pixel_rate = ((mode->pixel_clock + (1 << S) - 1) >> S) + mode->width;
  613. pixel_rate = mode->window_width * (pixel_rate - 1) / mode->width;
  614. __v4l2_ctrl_modify_range(ctrl, min, max, min, max);
  615. __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate, pixel_rate << S);
  616. }
  617. static int et8ek8_configure(struct et8ek8_sensor *sensor)
  618. {
  619. struct v4l2_subdev *subdev = &sensor->subdev;
  620. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  621. int rval;
  622. rval = et8ek8_i2c_write_regs(client, sensor->current_reglist->regs);
  623. if (rval)
  624. goto fail;
  625. /* Controls set while the power to the sensor is turned off are saved
  626. * but not applied to the hardware. Now that we're about to start
  627. * streaming apply all the current values to the hardware.
  628. */
  629. rval = v4l2_ctrl_handler_setup(&sensor->ctrl_handler);
  630. if (rval)
  631. goto fail;
  632. return 0;
  633. fail:
  634. dev_err(&client->dev, "sensor configuration failed\n");
  635. return rval;
  636. }
  637. static int et8ek8_stream_on(struct et8ek8_sensor *sensor)
  638. {
  639. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  640. return et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1252, 0xb0);
  641. }
  642. static int et8ek8_stream_off(struct et8ek8_sensor *sensor)
  643. {
  644. struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
  645. return et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1252, 0x30);
  646. }
  647. static int et8ek8_s_stream(struct v4l2_subdev *subdev, int streaming)
  648. {
  649. struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
  650. int ret;
  651. if (!streaming)
  652. return et8ek8_stream_off(sensor);
  653. ret = et8ek8_configure(sensor);
  654. if (ret < 0)
  655. return ret;
  656. return et8ek8_stream_on(sensor);
  657. }
  658. /* --------------------------------------------------------------------------
  659. * V4L2 subdev operations
  660. */
  661. static int et8ek8_power_off(struct et8ek8_sensor *sensor)
  662. {
  663. gpiod_set_value(sensor->reset, 0);
  664. udelay(1);
  665. clk_disable_unprepare(sensor->ext_clk);
  666. return regulator_disable(sensor->vana);
  667. }
  668. static int et8ek8_power_on(struct et8ek8_sensor *sensor)
  669. {
  670. struct v4l2_subdev *subdev = &sensor->subdev;
  671. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  672. unsigned int xclk_freq;
  673. int val, rval;
  674. rval = regulator_enable(sensor->vana);
  675. if (rval) {
  676. dev_err(&client->dev, "failed to enable vana regulator\n");
  677. return rval;
  678. }
  679. if (sensor->current_reglist)
  680. xclk_freq = sensor->current_reglist->mode.ext_clock;
  681. else
  682. xclk_freq = sensor->xclk_freq;
  683. rval = clk_set_rate(sensor->ext_clk, xclk_freq);
  684. if (rval < 0) {
  685. dev_err(&client->dev, "unable to set extclk clock freq to %u\n",
  686. xclk_freq);
  687. goto out;
  688. }
  689. rval = clk_prepare_enable(sensor->ext_clk);
  690. if (rval < 0) {
  691. dev_err(&client->dev, "failed to enable extclk\n");
  692. goto out;
  693. }
  694. if (rval)
  695. goto out;
  696. udelay(10); /* I wish this is a good value */
  697. gpiod_set_value(sensor->reset, 1);
  698. msleep(5000 * 1000 / xclk_freq + 1); /* Wait 5000 cycles */
  699. rval = et8ek8_i2c_reglist_find_write(client, &meta_reglist,
  700. ET8EK8_REGLIST_POWERON);
  701. if (rval)
  702. goto out;
  703. #ifdef USE_CRC
  704. rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT, 0x1263, &val);
  705. if (rval)
  706. goto out;
  707. #if USE_CRC /* TODO get crc setting from DT */
  708. val |= BIT(4);
  709. #else
  710. val &= ~BIT(4);
  711. #endif
  712. rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1263, val);
  713. if (rval)
  714. goto out;
  715. #endif
  716. out:
  717. if (rval)
  718. et8ek8_power_off(sensor);
  719. return rval;
  720. }
  721. /* --------------------------------------------------------------------------
  722. * V4L2 subdev video operations
  723. */
  724. #define MAX_FMTS 4
  725. static int et8ek8_enum_mbus_code(struct v4l2_subdev *subdev,
  726. struct v4l2_subdev_pad_config *cfg,
  727. struct v4l2_subdev_mbus_code_enum *code)
  728. {
  729. struct et8ek8_reglist **list =
  730. et8ek8_reglist_first(&meta_reglist);
  731. u32 pixelformat[MAX_FMTS];
  732. int npixelformat = 0;
  733. if (code->index >= MAX_FMTS)
  734. return -EINVAL;
  735. for (; *list; list++) {
  736. struct et8ek8_mode *mode = &(*list)->mode;
  737. int i;
  738. if ((*list)->type != ET8EK8_REGLIST_MODE)
  739. continue;
  740. for (i = 0; i < npixelformat; i++) {
  741. if (pixelformat[i] == mode->bus_format)
  742. break;
  743. }
  744. if (i != npixelformat)
  745. continue;
  746. if (code->index == npixelformat) {
  747. code->code = mode->bus_format;
  748. return 0;
  749. }
  750. pixelformat[npixelformat] = mode->bus_format;
  751. npixelformat++;
  752. }
  753. return -EINVAL;
  754. }
  755. static int et8ek8_enum_frame_size(struct v4l2_subdev *subdev,
  756. struct v4l2_subdev_pad_config *cfg,
  757. struct v4l2_subdev_frame_size_enum *fse)
  758. {
  759. struct et8ek8_reglist **list =
  760. et8ek8_reglist_first(&meta_reglist);
  761. struct v4l2_mbus_framefmt format;
  762. int cmp_width = INT_MAX;
  763. int cmp_height = INT_MAX;
  764. int index = fse->index;
  765. for (; *list; list++) {
  766. if ((*list)->type != ET8EK8_REGLIST_MODE)
  767. continue;
  768. et8ek8_reglist_to_mbus(*list, &format);
  769. if (fse->code != format.code)
  770. continue;
  771. /* Assume that the modes are grouped by frame size. */
  772. if (format.width == cmp_width && format.height == cmp_height)
  773. continue;
  774. cmp_width = format.width;
  775. cmp_height = format.height;
  776. if (index-- == 0) {
  777. fse->min_width = format.width;
  778. fse->min_height = format.height;
  779. fse->max_width = format.width;
  780. fse->max_height = format.height;
  781. return 0;
  782. }
  783. }
  784. return -EINVAL;
  785. }
  786. static int et8ek8_enum_frame_ival(struct v4l2_subdev *subdev,
  787. struct v4l2_subdev_pad_config *cfg,
  788. struct v4l2_subdev_frame_interval_enum *fie)
  789. {
  790. struct et8ek8_reglist **list =
  791. et8ek8_reglist_first(&meta_reglist);
  792. struct v4l2_mbus_framefmt format;
  793. int index = fie->index;
  794. for (; *list; list++) {
  795. struct et8ek8_mode *mode = &(*list)->mode;
  796. if ((*list)->type != ET8EK8_REGLIST_MODE)
  797. continue;
  798. et8ek8_reglist_to_mbus(*list, &format);
  799. if (fie->code != format.code)
  800. continue;
  801. if (fie->width != format.width || fie->height != format.height)
  802. continue;
  803. if (index-- == 0) {
  804. fie->interval = mode->timeperframe;
  805. return 0;
  806. }
  807. }
  808. return -EINVAL;
  809. }
  810. static struct v4l2_mbus_framefmt *
  811. __et8ek8_get_pad_format(struct et8ek8_sensor *sensor,
  812. struct v4l2_subdev_pad_config *cfg,
  813. unsigned int pad, enum v4l2_subdev_format_whence which)
  814. {
  815. switch (which) {
  816. case V4L2_SUBDEV_FORMAT_TRY:
  817. return v4l2_subdev_get_try_format(&sensor->subdev, cfg, pad);
  818. case V4L2_SUBDEV_FORMAT_ACTIVE:
  819. return &sensor->format;
  820. default:
  821. return NULL;
  822. }
  823. }
  824. static int et8ek8_get_pad_format(struct v4l2_subdev *subdev,
  825. struct v4l2_subdev_pad_config *cfg,
  826. struct v4l2_subdev_format *fmt)
  827. {
  828. struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
  829. struct v4l2_mbus_framefmt *format;
  830. format = __et8ek8_get_pad_format(sensor, cfg, fmt->pad, fmt->which);
  831. if (!format)
  832. return -EINVAL;
  833. fmt->format = *format;
  834. return 0;
  835. }
  836. static int et8ek8_set_pad_format(struct v4l2_subdev *subdev,
  837. struct v4l2_subdev_pad_config *cfg,
  838. struct v4l2_subdev_format *fmt)
  839. {
  840. struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
  841. struct v4l2_mbus_framefmt *format;
  842. struct et8ek8_reglist *reglist;
  843. format = __et8ek8_get_pad_format(sensor, cfg, fmt->pad, fmt->which);
  844. if (!format)
  845. return -EINVAL;
  846. reglist = et8ek8_reglist_find_mode_fmt(&meta_reglist, &fmt->format);
  847. et8ek8_reglist_to_mbus(reglist, &fmt->format);
  848. *format = fmt->format;
  849. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  850. sensor->current_reglist = reglist;
  851. et8ek8_update_controls(sensor);
  852. }
  853. return 0;
  854. }
  855. static int et8ek8_get_frame_interval(struct v4l2_subdev *subdev,
  856. struct v4l2_subdev_frame_interval *fi)
  857. {
  858. struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
  859. memset(fi, 0, sizeof(*fi));
  860. fi->interval = sensor->current_reglist->mode.timeperframe;
  861. return 0;
  862. }
  863. static int et8ek8_set_frame_interval(struct v4l2_subdev *subdev,
  864. struct v4l2_subdev_frame_interval *fi)
  865. {
  866. struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
  867. struct et8ek8_reglist *reglist;
  868. reglist = et8ek8_reglist_find_mode_ival(&meta_reglist,
  869. sensor->current_reglist,
  870. &fi->interval);
  871. if (!reglist)
  872. return -EINVAL;
  873. if (sensor->current_reglist->mode.ext_clock != reglist->mode.ext_clock)
  874. return -EINVAL;
  875. sensor->current_reglist = reglist;
  876. et8ek8_update_controls(sensor);
  877. return 0;
  878. }
  879. static int et8ek8_g_priv_mem(struct v4l2_subdev *subdev)
  880. {
  881. struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
  882. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  883. unsigned int length = ET8EK8_PRIV_MEM_SIZE;
  884. unsigned int offset = 0;
  885. u8 *ptr = sensor->priv_mem;
  886. int rval = 0;
  887. /* Read the EEPROM window-by-window, each window 8 bytes */
  888. do {
  889. u8 buffer[PRIV_MEM_WIN_SIZE];
  890. struct i2c_msg msg;
  891. int bytes, i;
  892. int ofs;
  893. /* Set the current window */
  894. rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x0001,
  895. 0xe0 | (offset >> 3));
  896. if (rval < 0)
  897. return rval;
  898. /* Wait for status bit */
  899. for (i = 0; i < 1000; ++i) {
  900. u32 status;
  901. rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT,
  902. 0x0003, &status);
  903. if (rval < 0)
  904. return rval;
  905. if (!(status & 0x08))
  906. break;
  907. usleep_range(1000, 2000);
  908. }
  909. if (i == 1000)
  910. return -EIO;
  911. /* Read window, 8 bytes at once, and copy to user space */
  912. ofs = offset & 0x07; /* Offset within this window */
  913. bytes = length + ofs > 8 ? 8-ofs : length;
  914. msg.addr = client->addr;
  915. msg.flags = 0;
  916. msg.len = 2;
  917. msg.buf = buffer;
  918. ofs += PRIV_MEM_START_REG;
  919. buffer[0] = (u8)(ofs >> 8);
  920. buffer[1] = (u8)(ofs & 0xFF);
  921. rval = i2c_transfer(client->adapter, &msg, 1);
  922. if (rval < 0)
  923. return rval;
  924. mdelay(ET8EK8_I2C_DELAY);
  925. msg.addr = client->addr;
  926. msg.len = bytes;
  927. msg.flags = I2C_M_RD;
  928. msg.buf = buffer;
  929. memset(buffer, 0, sizeof(buffer));
  930. rval = i2c_transfer(client->adapter, &msg, 1);
  931. if (rval < 0)
  932. return rval;
  933. rval = 0;
  934. memcpy(ptr, buffer, bytes);
  935. length -= bytes;
  936. offset += bytes;
  937. ptr += bytes;
  938. } while (length > 0);
  939. return rval;
  940. }
  941. static int et8ek8_dev_init(struct v4l2_subdev *subdev)
  942. {
  943. struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
  944. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  945. int rval, rev_l, rev_h;
  946. rval = et8ek8_power_on(sensor);
  947. if (rval) {
  948. dev_err(&client->dev, "could not power on\n");
  949. return rval;
  950. }
  951. rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT,
  952. REG_REVISION_NUMBER_L, &rev_l);
  953. if (!rval)
  954. rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT,
  955. REG_REVISION_NUMBER_H, &rev_h);
  956. if (rval) {
  957. dev_err(&client->dev, "no et8ek8 sensor detected\n");
  958. goto out_poweroff;
  959. }
  960. sensor->version = (rev_h << 8) + rev_l;
  961. if (sensor->version != ET8EK8_REV_1 && sensor->version != ET8EK8_REV_2)
  962. dev_info(&client->dev,
  963. "unknown version 0x%x detected, continuing anyway\n",
  964. sensor->version);
  965. rval = et8ek8_reglist_import(client, &meta_reglist);
  966. if (rval) {
  967. dev_err(&client->dev,
  968. "invalid register list %s, import failed\n",
  969. ET8EK8_NAME);
  970. goto out_poweroff;
  971. }
  972. sensor->current_reglist = et8ek8_reglist_find_type(&meta_reglist,
  973. ET8EK8_REGLIST_MODE);
  974. if (!sensor->current_reglist) {
  975. dev_err(&client->dev,
  976. "invalid register list %s, no mode found\n",
  977. ET8EK8_NAME);
  978. rval = -ENODEV;
  979. goto out_poweroff;
  980. }
  981. et8ek8_reglist_to_mbus(sensor->current_reglist, &sensor->format);
  982. rval = et8ek8_i2c_reglist_find_write(client, &meta_reglist,
  983. ET8EK8_REGLIST_POWERON);
  984. if (rval) {
  985. dev_err(&client->dev,
  986. "invalid register list %s, no POWERON mode found\n",
  987. ET8EK8_NAME);
  988. goto out_poweroff;
  989. }
  990. rval = et8ek8_stream_on(sensor); /* Needed to be able to read EEPROM */
  991. if (rval)
  992. goto out_poweroff;
  993. rval = et8ek8_g_priv_mem(subdev);
  994. if (rval)
  995. dev_warn(&client->dev,
  996. "can not read OTP (EEPROM) memory from sensor\n");
  997. rval = et8ek8_stream_off(sensor);
  998. if (rval)
  999. goto out_poweroff;
  1000. rval = et8ek8_power_off(sensor);
  1001. if (rval)
  1002. goto out_poweroff;
  1003. return 0;
  1004. out_poweroff:
  1005. et8ek8_power_off(sensor);
  1006. return rval;
  1007. }
  1008. /* --------------------------------------------------------------------------
  1009. * sysfs attributes
  1010. */
  1011. static ssize_t
  1012. et8ek8_priv_mem_read(struct device *dev, struct device_attribute *attr,
  1013. char *buf)
  1014. {
  1015. struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
  1016. struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
  1017. #if PAGE_SIZE < ET8EK8_PRIV_MEM_SIZE
  1018. #error PAGE_SIZE too small!
  1019. #endif
  1020. memcpy(buf, sensor->priv_mem, ET8EK8_PRIV_MEM_SIZE);
  1021. return ET8EK8_PRIV_MEM_SIZE;
  1022. }
  1023. static DEVICE_ATTR(priv_mem, 0444, et8ek8_priv_mem_read, NULL);
  1024. /* --------------------------------------------------------------------------
  1025. * V4L2 subdev core operations
  1026. */
  1027. static int
  1028. et8ek8_registered(struct v4l2_subdev *subdev)
  1029. {
  1030. struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
  1031. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  1032. int rval;
  1033. dev_dbg(&client->dev, "registered!");
  1034. rval = device_create_file(&client->dev, &dev_attr_priv_mem);
  1035. if (rval) {
  1036. dev_err(&client->dev, "could not register sysfs entry\n");
  1037. return rval;
  1038. }
  1039. rval = et8ek8_dev_init(subdev);
  1040. if (rval)
  1041. goto err_file;
  1042. rval = et8ek8_init_controls(sensor);
  1043. if (rval) {
  1044. dev_err(&client->dev, "controls initialization failed\n");
  1045. goto err_file;
  1046. }
  1047. __et8ek8_get_pad_format(sensor, NULL, 0, V4L2_SUBDEV_FORMAT_ACTIVE);
  1048. return 0;
  1049. err_file:
  1050. device_remove_file(&client->dev, &dev_attr_priv_mem);
  1051. return rval;
  1052. }
  1053. static int __et8ek8_set_power(struct et8ek8_sensor *sensor, bool on)
  1054. {
  1055. return on ? et8ek8_power_on(sensor) : et8ek8_power_off(sensor);
  1056. }
  1057. static int et8ek8_set_power(struct v4l2_subdev *subdev, int on)
  1058. {
  1059. struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
  1060. int ret = 0;
  1061. mutex_lock(&sensor->power_lock);
  1062. /* If the power count is modified from 0 to != 0 or from != 0 to 0,
  1063. * update the power state.
  1064. */
  1065. if (sensor->power_count == !on) {
  1066. ret = __et8ek8_set_power(sensor, !!on);
  1067. if (ret < 0)
  1068. goto done;
  1069. }
  1070. /* Update the power count. */
  1071. sensor->power_count += on ? 1 : -1;
  1072. WARN_ON(sensor->power_count < 0);
  1073. done:
  1074. mutex_unlock(&sensor->power_lock);
  1075. return ret;
  1076. }
  1077. static int et8ek8_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  1078. {
  1079. struct et8ek8_sensor *sensor = to_et8ek8_sensor(sd);
  1080. struct v4l2_mbus_framefmt *format;
  1081. struct et8ek8_reglist *reglist;
  1082. reglist = et8ek8_reglist_find_type(&meta_reglist, ET8EK8_REGLIST_MODE);
  1083. format = __et8ek8_get_pad_format(sensor, fh->pad, 0,
  1084. V4L2_SUBDEV_FORMAT_TRY);
  1085. et8ek8_reglist_to_mbus(reglist, format);
  1086. return et8ek8_set_power(sd, true);
  1087. }
  1088. static int et8ek8_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  1089. {
  1090. return et8ek8_set_power(sd, false);
  1091. }
  1092. static const struct v4l2_subdev_video_ops et8ek8_video_ops = {
  1093. .s_stream = et8ek8_s_stream,
  1094. .g_frame_interval = et8ek8_get_frame_interval,
  1095. .s_frame_interval = et8ek8_set_frame_interval,
  1096. };
  1097. static const struct v4l2_subdev_core_ops et8ek8_core_ops = {
  1098. .s_power = et8ek8_set_power,
  1099. };
  1100. static const struct v4l2_subdev_pad_ops et8ek8_pad_ops = {
  1101. .enum_mbus_code = et8ek8_enum_mbus_code,
  1102. .enum_frame_size = et8ek8_enum_frame_size,
  1103. .enum_frame_interval = et8ek8_enum_frame_ival,
  1104. .get_fmt = et8ek8_get_pad_format,
  1105. .set_fmt = et8ek8_set_pad_format,
  1106. };
  1107. static const struct v4l2_subdev_ops et8ek8_ops = {
  1108. .core = &et8ek8_core_ops,
  1109. .video = &et8ek8_video_ops,
  1110. .pad = &et8ek8_pad_ops,
  1111. };
  1112. static const struct v4l2_subdev_internal_ops et8ek8_internal_ops = {
  1113. .registered = et8ek8_registered,
  1114. .open = et8ek8_open,
  1115. .close = et8ek8_close,
  1116. };
  1117. /* --------------------------------------------------------------------------
  1118. * I2C driver
  1119. */
  1120. static int __maybe_unused et8ek8_suspend(struct device *dev)
  1121. {
  1122. struct i2c_client *client = to_i2c_client(dev);
  1123. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  1124. struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
  1125. if (!sensor->power_count)
  1126. return 0;
  1127. return __et8ek8_set_power(sensor, false);
  1128. }
  1129. static int __maybe_unused et8ek8_resume(struct device *dev)
  1130. {
  1131. struct i2c_client *client = to_i2c_client(dev);
  1132. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  1133. struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
  1134. if (!sensor->power_count)
  1135. return 0;
  1136. return __et8ek8_set_power(sensor, true);
  1137. }
  1138. static int et8ek8_probe(struct i2c_client *client,
  1139. const struct i2c_device_id *devid)
  1140. {
  1141. struct et8ek8_sensor *sensor;
  1142. struct device *dev = &client->dev;
  1143. int ret;
  1144. sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
  1145. if (!sensor)
  1146. return -ENOMEM;
  1147. sensor->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
  1148. if (IS_ERR(sensor->reset)) {
  1149. dev_dbg(&client->dev, "could not request reset gpio\n");
  1150. return PTR_ERR(sensor->reset);
  1151. }
  1152. sensor->vana = devm_regulator_get(dev, "vana");
  1153. if (IS_ERR(sensor->vana)) {
  1154. dev_err(&client->dev, "could not get regulator for vana\n");
  1155. return PTR_ERR(sensor->vana);
  1156. }
  1157. sensor->ext_clk = devm_clk_get(dev, NULL);
  1158. if (IS_ERR(sensor->ext_clk)) {
  1159. dev_err(&client->dev, "could not get clock\n");
  1160. return PTR_ERR(sensor->ext_clk);
  1161. }
  1162. ret = of_property_read_u32(dev->of_node, "clock-frequency",
  1163. &sensor->xclk_freq);
  1164. if (ret) {
  1165. dev_warn(dev, "can't get clock-frequency\n");
  1166. return ret;
  1167. }
  1168. mutex_init(&sensor->power_lock);
  1169. v4l2_i2c_subdev_init(&sensor->subdev, client, &et8ek8_ops);
  1170. sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1171. sensor->subdev.internal_ops = &et8ek8_internal_ops;
  1172. sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
  1173. ret = media_entity_pads_init(&sensor->subdev.entity, 1, &sensor->pad);
  1174. if (ret < 0) {
  1175. dev_err(&client->dev, "media entity init failed!\n");
  1176. goto err_mutex;
  1177. }
  1178. ret = v4l2_async_register_subdev(&sensor->subdev);
  1179. if (ret < 0)
  1180. goto err_entity;
  1181. dev_dbg(dev, "initialized!\n");
  1182. return 0;
  1183. err_entity:
  1184. media_entity_cleanup(&sensor->subdev.entity);
  1185. err_mutex:
  1186. mutex_destroy(&sensor->power_lock);
  1187. return ret;
  1188. }
  1189. static int __exit et8ek8_remove(struct i2c_client *client)
  1190. {
  1191. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  1192. struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
  1193. if (sensor->power_count) {
  1194. WARN_ON(1);
  1195. et8ek8_power_off(sensor);
  1196. sensor->power_count = 0;
  1197. }
  1198. v4l2_device_unregister_subdev(&sensor->subdev);
  1199. device_remove_file(&client->dev, &dev_attr_priv_mem);
  1200. v4l2_ctrl_handler_free(&sensor->ctrl_handler);
  1201. v4l2_async_unregister_subdev(&sensor->subdev);
  1202. media_entity_cleanup(&sensor->subdev.entity);
  1203. mutex_destroy(&sensor->power_lock);
  1204. return 0;
  1205. }
  1206. static const struct of_device_id et8ek8_of_table[] = {
  1207. { .compatible = "toshiba,et8ek8" },
  1208. { },
  1209. };
  1210. MODULE_DEVICE_TABLE(of, et8ek8_of_table);
  1211. static const struct i2c_device_id et8ek8_id_table[] = {
  1212. { ET8EK8_NAME, 0 },
  1213. { }
  1214. };
  1215. MODULE_DEVICE_TABLE(i2c, et8ek8_id_table);
  1216. static const struct dev_pm_ops et8ek8_pm_ops = {
  1217. SET_SYSTEM_SLEEP_PM_OPS(et8ek8_suspend, et8ek8_resume)
  1218. };
  1219. MODULE_DEVICE_TABLE(of, et8ek8_of_table);
  1220. static struct i2c_driver et8ek8_i2c_driver = {
  1221. .driver = {
  1222. .name = ET8EK8_NAME,
  1223. .pm = &et8ek8_pm_ops,
  1224. .of_match_table = et8ek8_of_table,
  1225. },
  1226. .probe = et8ek8_probe,
  1227. .remove = __exit_p(et8ek8_remove),
  1228. .id_table = et8ek8_id_table,
  1229. };
  1230. module_i2c_driver(et8ek8_i2c_driver);
  1231. MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>, Pavel Machek <pavel@ucw.cz");
  1232. MODULE_DESCRIPTION("Toshiba ET8EK8 camera sensor driver");
  1233. MODULE_LICENSE("GPL");