et8ek8_driver.c 35 KB

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