max9286.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Maxim MAX9286 GMSL Deserializer Driver
  4. *
  5. * Copyright (C) 2017-2019 Jacopo Mondi
  6. * Copyright (C) 2017-2019 Kieran Bingham
  7. * Copyright (C) 2017-2019 Laurent Pinchart
  8. * Copyright (C) 2017-2019 Niklas Söderlund
  9. * Copyright (C) 2016 Renesas Electronics Corporation
  10. * Copyright (C) 2015 Cogent Embedded, Inc.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/fwnode.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/gpio/driver.h>
  17. #include <linux/gpio/machine.h>
  18. #include <linux/i2c.h>
  19. #include <linux/i2c-mux.h>
  20. #include <linux/module.h>
  21. #include <linux/of_graph.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/slab.h>
  24. #include <media/v4l2-async.h>
  25. #include <media/v4l2-ctrls.h>
  26. #include <media/v4l2-device.h>
  27. #include <media/v4l2-fwnode.h>
  28. #include <media/v4l2-subdev.h>
  29. /* Register 0x00 */
  30. #define MAX9286_MSTLINKSEL_AUTO (7 << 5)
  31. #define MAX9286_MSTLINKSEL(n) ((n) << 5)
  32. #define MAX9286_EN_VS_GEN BIT(4)
  33. #define MAX9286_LINKEN(n) (1 << (n))
  34. /* Register 0x01 */
  35. #define MAX9286_FSYNCMODE_ECU (3 << 6)
  36. #define MAX9286_FSYNCMODE_EXT (2 << 6)
  37. #define MAX9286_FSYNCMODE_INT_OUT (1 << 6)
  38. #define MAX9286_FSYNCMODE_INT_HIZ (0 << 6)
  39. #define MAX9286_GPIEN BIT(5)
  40. #define MAX9286_ENLMO_RSTFSYNC BIT(2)
  41. #define MAX9286_FSYNCMETH_AUTO (2 << 0)
  42. #define MAX9286_FSYNCMETH_SEMI_AUTO (1 << 0)
  43. #define MAX9286_FSYNCMETH_MANUAL (0 << 0)
  44. #define MAX9286_REG_FSYNC_PERIOD_L 0x06
  45. #define MAX9286_REG_FSYNC_PERIOD_M 0x07
  46. #define MAX9286_REG_FSYNC_PERIOD_H 0x08
  47. /* Register 0x0a */
  48. #define MAX9286_FWDCCEN(n) (1 << ((n) + 4))
  49. #define MAX9286_REVCCEN(n) (1 << (n))
  50. /* Register 0x0c */
  51. #define MAX9286_HVEN BIT(7)
  52. #define MAX9286_EDC_6BIT_HAMMING (2 << 5)
  53. #define MAX9286_EDC_6BIT_CRC (1 << 5)
  54. #define MAX9286_EDC_1BIT_PARITY (0 << 5)
  55. #define MAX9286_DESEL BIT(4)
  56. #define MAX9286_INVVS BIT(3)
  57. #define MAX9286_INVHS BIT(2)
  58. #define MAX9286_HVSRC_D0 (2 << 0)
  59. #define MAX9286_HVSRC_D14 (1 << 0)
  60. #define MAX9286_HVSRC_D18 (0 << 0)
  61. /* Register 0x0f */
  62. #define MAX9286_0X0F_RESERVED BIT(3)
  63. /* Register 0x12 */
  64. #define MAX9286_CSILANECNT(n) (((n) - 1) << 6)
  65. #define MAX9286_CSIDBL BIT(5)
  66. #define MAX9286_DBL BIT(4)
  67. #define MAX9286_DATATYPE_USER_8BIT (11 << 0)
  68. #define MAX9286_DATATYPE_USER_YUV_12BIT (10 << 0)
  69. #define MAX9286_DATATYPE_USER_24BIT (9 << 0)
  70. #define MAX9286_DATATYPE_RAW14 (8 << 0)
  71. #define MAX9286_DATATYPE_RAW12 (7 << 0)
  72. #define MAX9286_DATATYPE_RAW10 (6 << 0)
  73. #define MAX9286_DATATYPE_RAW8 (5 << 0)
  74. #define MAX9286_DATATYPE_YUV422_10BIT (4 << 0)
  75. #define MAX9286_DATATYPE_YUV422_8BIT (3 << 0)
  76. #define MAX9286_DATATYPE_RGB555 (2 << 0)
  77. #define MAX9286_DATATYPE_RGB565 (1 << 0)
  78. #define MAX9286_DATATYPE_RGB888 (0 << 0)
  79. /* Register 0x15 */
  80. #define MAX9286_CSI_IMAGE_TYP BIT(7)
  81. #define MAX9286_VC(n) ((n) << 5)
  82. #define MAX9286_VCTYPE BIT(4)
  83. #define MAX9286_CSIOUTEN BIT(3)
  84. #define MAX9286_SWP_ENDIAN BIT(2)
  85. #define MAX9286_EN_CCBSYB_CLK_STR BIT(1)
  86. #define MAX9286_EN_GPI_CCBSYB BIT(0)
  87. /* Register 0x1b */
  88. #define MAX9286_SWITCHIN(n) (1 << ((n) + 4))
  89. #define MAX9286_ENEQ(n) (1 << (n))
  90. /* Register 0x1c */
  91. #define MAX9286_HIGHIMM(n) BIT((n) + 4)
  92. #define MAX9286_I2CSEL BIT(2)
  93. #define MAX9286_HIBW BIT(1)
  94. #define MAX9286_BWS BIT(0)
  95. /* Register 0x27 */
  96. #define MAX9286_LOCKED BIT(7)
  97. /* Register 0x31 */
  98. #define MAX9286_FSYNC_LOCKED BIT(6)
  99. /* Register 0x34 */
  100. #define MAX9286_I2CLOCACK BIT(7)
  101. #define MAX9286_I2CSLVSH_1046NS_469NS (3 << 5)
  102. #define MAX9286_I2CSLVSH_938NS_352NS (2 << 5)
  103. #define MAX9286_I2CSLVSH_469NS_234NS (1 << 5)
  104. #define MAX9286_I2CSLVSH_352NS_117NS (0 << 5)
  105. #define MAX9286_I2CMSTBT_837KBPS (7 << 2)
  106. #define MAX9286_I2CMSTBT_533KBPS (6 << 2)
  107. #define MAX9286_I2CMSTBT_339KBPS (5 << 2)
  108. #define MAX9286_I2CMSTBT_173KBPS (4 << 2)
  109. #define MAX9286_I2CMSTBT_105KBPS (3 << 2)
  110. #define MAX9286_I2CMSTBT_84KBPS (2 << 2)
  111. #define MAX9286_I2CMSTBT_28KBPS (1 << 2)
  112. #define MAX9286_I2CMSTBT_8KBPS (0 << 2)
  113. #define MAX9286_I2CSLVTO_NONE (3 << 0)
  114. #define MAX9286_I2CSLVTO_1024US (2 << 0)
  115. #define MAX9286_I2CSLVTO_256US (1 << 0)
  116. #define MAX9286_I2CSLVTO_64US (0 << 0)
  117. /* Register 0x3b */
  118. #define MAX9286_REV_TRF(n) ((n) << 4)
  119. #define MAX9286_REV_AMP(n) ((((n) - 30) / 10) << 1) /* in mV */
  120. #define MAX9286_REV_AMP_X BIT(0)
  121. #define MAX9286_REV_AMP_HIGH 170
  122. /* Register 0x3f */
  123. #define MAX9286_EN_REV_CFG BIT(6)
  124. #define MAX9286_REV_FLEN(n) ((n) - 20)
  125. /* Register 0x49 */
  126. #define MAX9286_VIDEO_DETECT_MASK 0x0f
  127. /* Register 0x69 */
  128. #define MAX9286_LFLTBMONMASKED BIT(7)
  129. #define MAX9286_LOCKMONMASKED BIT(6)
  130. #define MAX9286_AUTOCOMBACKEN BIT(5)
  131. #define MAX9286_AUTOMASKEN BIT(4)
  132. #define MAX9286_MASKLINK(n) ((n) << 0)
  133. /*
  134. * The sink and source pads are created to match the OF graph port numbers so
  135. * that their indexes can be used interchangeably.
  136. */
  137. #define MAX9286_NUM_GMSL 4
  138. #define MAX9286_N_SINKS 4
  139. #define MAX9286_N_PADS 5
  140. #define MAX9286_SRC_PAD 4
  141. struct max9286_format_info {
  142. u32 code;
  143. u8 datatype;
  144. };
  145. struct max9286_i2c_speed {
  146. u32 rate;
  147. u8 mstbt;
  148. };
  149. struct max9286_source {
  150. struct v4l2_subdev *sd;
  151. struct fwnode_handle *fwnode;
  152. struct regulator *regulator;
  153. };
  154. struct max9286_asd {
  155. struct v4l2_async_connection base;
  156. struct max9286_source *source;
  157. };
  158. static inline struct max9286_asd *
  159. to_max9286_asd(struct v4l2_async_connection *asd)
  160. {
  161. return container_of(asd, struct max9286_asd, base);
  162. }
  163. struct max9286_priv {
  164. struct i2c_client *client;
  165. struct gpio_desc *gpiod_pwdn;
  166. struct v4l2_subdev sd;
  167. struct media_pad pads[MAX9286_N_PADS];
  168. struct regulator *regulator;
  169. struct gpio_chip gpio;
  170. u8 gpio_state;
  171. struct i2c_mux_core *mux;
  172. unsigned int mux_channel;
  173. bool mux_open;
  174. /* The initial reverse control channel amplitude. */
  175. u32 init_rev_chan_mv;
  176. u32 rev_chan_mv;
  177. u8 i2c_mstbt;
  178. u32 bus_width;
  179. bool use_gpio_poc;
  180. u32 gpio_poc[2];
  181. struct v4l2_ctrl_handler ctrls;
  182. struct v4l2_ctrl *pixelrate_ctrl;
  183. unsigned int pixelrate;
  184. unsigned int nsources;
  185. unsigned int source_mask;
  186. unsigned int route_mask;
  187. unsigned int bound_sources;
  188. unsigned int csi2_data_lanes;
  189. struct max9286_source sources[MAX9286_NUM_GMSL];
  190. struct v4l2_async_notifier notifier;
  191. };
  192. static struct max9286_source *next_source(struct max9286_priv *priv,
  193. struct max9286_source *source)
  194. {
  195. if (!source)
  196. source = &priv->sources[0];
  197. else
  198. source++;
  199. for (; source < &priv->sources[MAX9286_NUM_GMSL]; source++) {
  200. if (source->fwnode)
  201. return source;
  202. }
  203. return NULL;
  204. }
  205. #define for_each_source(priv, source) \
  206. for ((source) = NULL; ((source) = next_source((priv), (source))); )
  207. #define to_index(priv, source) ((source) - &(priv)->sources[0])
  208. static inline struct max9286_priv *sd_to_max9286(struct v4l2_subdev *sd)
  209. {
  210. return container_of(sd, struct max9286_priv, sd);
  211. }
  212. static const struct max9286_format_info max9286_formats[] = {
  213. {
  214. .code = MEDIA_BUS_FMT_UYVY8_1X16,
  215. .datatype = MAX9286_DATATYPE_YUV422_8BIT,
  216. }, {
  217. .code = MEDIA_BUS_FMT_VYUY8_1X16,
  218. .datatype = MAX9286_DATATYPE_YUV422_8BIT,
  219. }, {
  220. .code = MEDIA_BUS_FMT_YUYV8_1X16,
  221. .datatype = MAX9286_DATATYPE_YUV422_8BIT,
  222. }, {
  223. .code = MEDIA_BUS_FMT_YVYU8_1X16,
  224. .datatype = MAX9286_DATATYPE_YUV422_8BIT,
  225. }, {
  226. .code = MEDIA_BUS_FMT_SBGGR12_1X12,
  227. .datatype = MAX9286_DATATYPE_RAW12,
  228. }, {
  229. .code = MEDIA_BUS_FMT_SGBRG12_1X12,
  230. .datatype = MAX9286_DATATYPE_RAW12,
  231. }, {
  232. .code = MEDIA_BUS_FMT_SGRBG12_1X12,
  233. .datatype = MAX9286_DATATYPE_RAW12,
  234. }, {
  235. .code = MEDIA_BUS_FMT_SRGGB12_1X12,
  236. .datatype = MAX9286_DATATYPE_RAW12,
  237. },
  238. };
  239. static const struct max9286_i2c_speed max9286_i2c_speeds[] = {
  240. { .rate = 8470, .mstbt = MAX9286_I2CMSTBT_8KBPS },
  241. { .rate = 28300, .mstbt = MAX9286_I2CMSTBT_28KBPS },
  242. { .rate = 84700, .mstbt = MAX9286_I2CMSTBT_84KBPS },
  243. { .rate = 105000, .mstbt = MAX9286_I2CMSTBT_105KBPS },
  244. { .rate = 173000, .mstbt = MAX9286_I2CMSTBT_173KBPS },
  245. { .rate = 339000, .mstbt = MAX9286_I2CMSTBT_339KBPS },
  246. { .rate = 533000, .mstbt = MAX9286_I2CMSTBT_533KBPS },
  247. { .rate = 837000, .mstbt = MAX9286_I2CMSTBT_837KBPS },
  248. };
  249. /* -----------------------------------------------------------------------------
  250. * I2C IO
  251. */
  252. static int max9286_read(struct max9286_priv *priv, u8 reg)
  253. {
  254. int ret;
  255. ret = i2c_smbus_read_byte_data(priv->client, reg);
  256. if (ret < 0)
  257. dev_err(&priv->client->dev,
  258. "%s: register 0x%02x read failed (%d)\n",
  259. __func__, reg, ret);
  260. return ret;
  261. }
  262. static int max9286_write(struct max9286_priv *priv, u8 reg, u8 val)
  263. {
  264. int ret;
  265. ret = i2c_smbus_write_byte_data(priv->client, reg, val);
  266. if (ret < 0)
  267. dev_err(&priv->client->dev,
  268. "%s: register 0x%02x write failed (%d)\n",
  269. __func__, reg, ret);
  270. return ret;
  271. }
  272. /* -----------------------------------------------------------------------------
  273. * I2C Multiplexer
  274. */
  275. static void max9286_i2c_mux_configure(struct max9286_priv *priv, u8 conf)
  276. {
  277. max9286_write(priv, 0x0a, conf);
  278. /*
  279. * We must sleep after any change to the forward or reverse channel
  280. * configuration.
  281. */
  282. usleep_range(3000, 5000);
  283. }
  284. static void max9286_i2c_mux_open(struct max9286_priv *priv)
  285. {
  286. /* Open all channels on the MAX9286 */
  287. max9286_i2c_mux_configure(priv, 0xff);
  288. priv->mux_open = true;
  289. }
  290. static void max9286_i2c_mux_close(struct max9286_priv *priv)
  291. {
  292. /*
  293. * Ensure that both the forward and reverse channel are disabled on the
  294. * mux, and that the channel ID is invalidated to ensure we reconfigure
  295. * on the next max9286_i2c_mux_select() call.
  296. */
  297. max9286_i2c_mux_configure(priv, 0x00);
  298. priv->mux_open = false;
  299. priv->mux_channel = -1;
  300. }
  301. static int max9286_i2c_mux_select(struct i2c_mux_core *muxc, u32 chan)
  302. {
  303. struct max9286_priv *priv = i2c_mux_priv(muxc);
  304. /* Channel select is disabled when configured in the opened state. */
  305. if (priv->mux_open)
  306. return 0;
  307. if (priv->mux_channel == chan)
  308. return 0;
  309. priv->mux_channel = chan;
  310. max9286_i2c_mux_configure(priv, MAX9286_FWDCCEN(chan) |
  311. MAX9286_REVCCEN(chan));
  312. return 0;
  313. }
  314. static int max9286_i2c_mux_init(struct max9286_priv *priv)
  315. {
  316. struct max9286_source *source;
  317. int ret;
  318. if (!i2c_check_functionality(priv->client->adapter,
  319. I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  320. return -ENODEV;
  321. priv->mux = i2c_mux_alloc(priv->client->adapter, &priv->client->dev,
  322. priv->nsources, 0, I2C_MUX_LOCKED,
  323. max9286_i2c_mux_select, NULL);
  324. if (!priv->mux)
  325. return -ENOMEM;
  326. priv->mux->priv = priv;
  327. for_each_source(priv, source) {
  328. unsigned int index = to_index(priv, source);
  329. ret = i2c_mux_add_adapter(priv->mux, 0, index);
  330. if (ret < 0)
  331. goto error;
  332. }
  333. return 0;
  334. error:
  335. i2c_mux_del_adapters(priv->mux);
  336. return ret;
  337. }
  338. static void max9286_configure_i2c(struct max9286_priv *priv, bool localack)
  339. {
  340. u8 config = MAX9286_I2CSLVSH_469NS_234NS | MAX9286_I2CSLVTO_1024US |
  341. priv->i2c_mstbt;
  342. if (localack)
  343. config |= MAX9286_I2CLOCACK;
  344. max9286_write(priv, 0x34, config);
  345. usleep_range(3000, 5000);
  346. }
  347. static void max9286_reverse_channel_setup(struct max9286_priv *priv,
  348. unsigned int chan_amplitude)
  349. {
  350. u8 chan_config;
  351. if (priv->rev_chan_mv == chan_amplitude)
  352. return;
  353. priv->rev_chan_mv = chan_amplitude;
  354. /* Reverse channel transmission time: default to 1. */
  355. chan_config = MAX9286_REV_TRF(1);
  356. /*
  357. * Reverse channel setup.
  358. *
  359. * - Enable custom reverse channel configuration (through register 0x3f)
  360. * and set the first pulse length to 35 clock cycles.
  361. * - Adjust reverse channel amplitude: values > 130 are programmed
  362. * using the additional +100mV REV_AMP_X boost flag
  363. */
  364. max9286_write(priv, 0x3f, MAX9286_EN_REV_CFG | MAX9286_REV_FLEN(35));
  365. if (chan_amplitude > 100) {
  366. /* It is not possible to express values (100 < x < 130) */
  367. chan_amplitude = max(30U, chan_amplitude - 100);
  368. chan_config |= MAX9286_REV_AMP_X;
  369. }
  370. max9286_write(priv, 0x3b, chan_config | MAX9286_REV_AMP(chan_amplitude));
  371. usleep_range(2000, 2500);
  372. }
  373. /*
  374. * max9286_check_video_links() - Make sure video links are detected and locked
  375. *
  376. * Performs safety checks on video link status. Make sure they are detected
  377. * and all enabled links are locked.
  378. *
  379. * Returns 0 for success, -EIO for errors.
  380. */
  381. static int max9286_check_video_links(struct max9286_priv *priv)
  382. {
  383. unsigned int i;
  384. int ret;
  385. /*
  386. * Make sure valid video links are detected.
  387. * The delay is not characterized in de-serializer manual, wait up
  388. * to 5 ms.
  389. */
  390. for (i = 0; i < 10; i++) {
  391. ret = max9286_read(priv, 0x49);
  392. if (ret < 0)
  393. return -EIO;
  394. if ((ret & MAX9286_VIDEO_DETECT_MASK) == priv->source_mask)
  395. break;
  396. usleep_range(350, 500);
  397. }
  398. if (i == 10) {
  399. dev_err(&priv->client->dev,
  400. "Unable to detect video links: 0x%02x\n", ret);
  401. return -EIO;
  402. }
  403. /* Make sure all enabled links are locked (4ms max). */
  404. for (i = 0; i < 10; i++) {
  405. ret = max9286_read(priv, 0x27);
  406. if (ret < 0)
  407. return -EIO;
  408. if (ret & MAX9286_LOCKED)
  409. break;
  410. usleep_range(350, 450);
  411. }
  412. if (i == 10) {
  413. dev_err(&priv->client->dev, "Not all enabled links locked\n");
  414. return -EIO;
  415. }
  416. return 0;
  417. }
  418. /*
  419. * max9286_check_config_link() - Detect and wait for configuration links
  420. *
  421. * Determine if the configuration channel is up and settled for a link.
  422. *
  423. * Returns 0 for success, -EIO for errors.
  424. */
  425. static int max9286_check_config_link(struct max9286_priv *priv,
  426. unsigned int source_mask)
  427. {
  428. unsigned int conflink_mask = (source_mask & 0x0f) << 4;
  429. unsigned int i;
  430. int ret;
  431. /*
  432. * Make sure requested configuration links are detected.
  433. * The delay is not characterized in the chip manual: wait up
  434. * to 5 milliseconds.
  435. */
  436. for (i = 0; i < 10; i++) {
  437. ret = max9286_read(priv, 0x49);
  438. if (ret < 0)
  439. return -EIO;
  440. ret &= 0xf0;
  441. if (ret == conflink_mask)
  442. break;
  443. usleep_range(350, 500);
  444. }
  445. if (ret != conflink_mask) {
  446. dev_err(&priv->client->dev,
  447. "Unable to detect configuration links: 0x%02x expected 0x%02x\n",
  448. ret, conflink_mask);
  449. return -EIO;
  450. }
  451. dev_info(&priv->client->dev,
  452. "Successfully detected configuration links after %u loops: 0x%02x\n",
  453. i, conflink_mask);
  454. return 0;
  455. }
  456. static void max9286_set_video_format(struct max9286_priv *priv,
  457. const struct v4l2_mbus_framefmt *format)
  458. {
  459. const struct max9286_format_info *info = NULL;
  460. unsigned int i;
  461. for (i = 0; i < ARRAY_SIZE(max9286_formats); ++i) {
  462. if (max9286_formats[i].code == format->code) {
  463. info = &max9286_formats[i];
  464. break;
  465. }
  466. }
  467. if (WARN_ON(!info))
  468. return;
  469. /*
  470. * Video format setup: disable CSI output, set VC according to Link
  471. * number, enable I2C clock stretching when CCBSY is low, enable CCBSY
  472. * in external GPI-to-GPO mode.
  473. */
  474. max9286_write(priv, 0x15, MAX9286_VCTYPE | MAX9286_EN_CCBSYB_CLK_STR |
  475. MAX9286_EN_GPI_CCBSYB);
  476. /* Enable CSI-2 Lane D0-D3 only, DBL mode. */
  477. max9286_write(priv, 0x12, MAX9286_CSIDBL | MAX9286_DBL |
  478. MAX9286_CSILANECNT(priv->csi2_data_lanes) |
  479. info->datatype);
  480. /*
  481. * Enable HS/VS encoding, use HS as line valid source, use D14/15 for
  482. * HS/VS, invert VS.
  483. */
  484. max9286_write(priv, 0x0c, MAX9286_HVEN | MAX9286_DESEL |
  485. MAX9286_INVVS | MAX9286_HVSRC_D14);
  486. }
  487. static void max9286_set_fsync_period(struct max9286_priv *priv,
  488. struct v4l2_subdev_state *state)
  489. {
  490. const struct v4l2_fract *interval;
  491. u32 fsync;
  492. interval = v4l2_subdev_state_get_interval(state, MAX9286_SRC_PAD);
  493. if (!interval->numerator || !interval->denominator) {
  494. /*
  495. * Special case, a null interval enables automatic FRAMESYNC
  496. * mode. FRAMESYNC is taken from the slowest link.
  497. */
  498. max9286_write(priv, 0x01, MAX9286_FSYNCMODE_INT_HIZ |
  499. MAX9286_FSYNCMETH_AUTO);
  500. return;
  501. }
  502. /*
  503. * Manual FRAMESYNC
  504. *
  505. * The FRAMESYNC generator is configured with a period expressed as a
  506. * number of PCLK periods.
  507. */
  508. fsync = div_u64((u64)priv->pixelrate * interval->numerator,
  509. interval->denominator);
  510. dev_dbg(&priv->client->dev, "fsync period %u (pclk %u)\n", fsync,
  511. priv->pixelrate);
  512. max9286_write(priv, 0x01, MAX9286_FSYNCMODE_INT_OUT |
  513. MAX9286_FSYNCMETH_MANUAL);
  514. max9286_write(priv, 0x06, (fsync >> 0) & 0xff);
  515. max9286_write(priv, 0x07, (fsync >> 8) & 0xff);
  516. max9286_write(priv, 0x08, (fsync >> 16) & 0xff);
  517. }
  518. /* -----------------------------------------------------------------------------
  519. * V4L2 Subdev
  520. */
  521. static int max9286_set_pixelrate(struct max9286_priv *priv)
  522. {
  523. struct max9286_source *source = NULL;
  524. u64 pixelrate = 0;
  525. for_each_source(priv, source) {
  526. struct v4l2_ctrl *ctrl;
  527. u64 source_rate = 0;
  528. /* Pixel rate is mandatory to be reported by sources. */
  529. ctrl = v4l2_ctrl_find(source->sd->ctrl_handler,
  530. V4L2_CID_PIXEL_RATE);
  531. if (!ctrl) {
  532. pixelrate = 0;
  533. break;
  534. }
  535. /* All source must report the same pixel rate. */
  536. source_rate = v4l2_ctrl_g_ctrl_int64(ctrl);
  537. if (!pixelrate) {
  538. pixelrate = source_rate;
  539. } else if (pixelrate != source_rate) {
  540. dev_err(&priv->client->dev,
  541. "Unable to calculate pixel rate\n");
  542. return -EINVAL;
  543. }
  544. }
  545. if (!pixelrate) {
  546. dev_err(&priv->client->dev,
  547. "No pixel rate control available in sources\n");
  548. return -EINVAL;
  549. }
  550. priv->pixelrate = pixelrate;
  551. /*
  552. * The CSI-2 transmitter pixel rate is the single source rate multiplied
  553. * by the number of available sources.
  554. */
  555. return v4l2_ctrl_s_ctrl_int64(priv->pixelrate_ctrl,
  556. pixelrate * priv->nsources);
  557. }
  558. static int max9286_notify_bound(struct v4l2_async_notifier *notifier,
  559. struct v4l2_subdev *subdev,
  560. struct v4l2_async_connection *asd)
  561. {
  562. struct max9286_priv *priv = sd_to_max9286(notifier->sd);
  563. struct max9286_source *source = to_max9286_asd(asd)->source;
  564. unsigned int index = to_index(priv, source);
  565. unsigned int src_pad;
  566. int ret;
  567. ret = media_entity_get_fwnode_pad(&subdev->entity,
  568. source->fwnode,
  569. MEDIA_PAD_FL_SOURCE);
  570. if (ret < 0) {
  571. dev_err(&priv->client->dev,
  572. "Failed to find pad for %s\n", subdev->name);
  573. return ret;
  574. }
  575. priv->bound_sources |= BIT(index);
  576. source->sd = subdev;
  577. src_pad = ret;
  578. ret = media_create_pad_link(&source->sd->entity, src_pad,
  579. &priv->sd.entity, index,
  580. MEDIA_LNK_FL_ENABLED |
  581. MEDIA_LNK_FL_IMMUTABLE);
  582. if (ret) {
  583. dev_err(&priv->client->dev,
  584. "Unable to link %s:%u -> %s:%u\n",
  585. source->sd->name, src_pad, priv->sd.name, index);
  586. return ret;
  587. }
  588. dev_dbg(&priv->client->dev, "Bound %s pad: %u on index %u\n",
  589. subdev->name, src_pad, index);
  590. /*
  591. * As we register a subdev notifiers we won't get a .complete() callback
  592. * here, so we have to use bound_sources to identify when all remote
  593. * serializers have probed.
  594. */
  595. if (priv->bound_sources != priv->source_mask)
  596. return 0;
  597. /*
  598. * All enabled sources have probed and enabled their reverse control
  599. * channels:
  600. *
  601. * - Increase the reverse channel amplitude to compensate for the
  602. * remote ends high threshold
  603. * - Verify all configuration links are properly detected
  604. * - Disable auto-ack as communication on the control channel are now
  605. * stable.
  606. */
  607. max9286_reverse_channel_setup(priv, MAX9286_REV_AMP_HIGH);
  608. max9286_check_config_link(priv, priv->source_mask);
  609. max9286_configure_i2c(priv, false);
  610. return max9286_set_pixelrate(priv);
  611. }
  612. static void max9286_notify_unbind(struct v4l2_async_notifier *notifier,
  613. struct v4l2_subdev *subdev,
  614. struct v4l2_async_connection *asd)
  615. {
  616. struct max9286_priv *priv = sd_to_max9286(notifier->sd);
  617. struct max9286_source *source = to_max9286_asd(asd)->source;
  618. unsigned int index = to_index(priv, source);
  619. source->sd = NULL;
  620. priv->bound_sources &= ~BIT(index);
  621. }
  622. static const struct v4l2_async_notifier_operations max9286_notify_ops = {
  623. .bound = max9286_notify_bound,
  624. .unbind = max9286_notify_unbind,
  625. };
  626. static int max9286_v4l2_notifier_register(struct max9286_priv *priv)
  627. {
  628. struct device *dev = &priv->client->dev;
  629. struct max9286_source *source = NULL;
  630. int ret;
  631. if (!priv->nsources)
  632. return 0;
  633. v4l2_async_subdev_nf_init(&priv->notifier, &priv->sd);
  634. for_each_source(priv, source) {
  635. unsigned int i = to_index(priv, source);
  636. struct max9286_asd *mas;
  637. mas = v4l2_async_nf_add_fwnode(&priv->notifier, source->fwnode,
  638. struct max9286_asd);
  639. if (IS_ERR(mas)) {
  640. dev_err(dev, "Failed to add subdev for source %u: %pe",
  641. i, mas);
  642. v4l2_async_nf_cleanup(&priv->notifier);
  643. return PTR_ERR(mas);
  644. }
  645. mas->source = source;
  646. }
  647. priv->notifier.ops = &max9286_notify_ops;
  648. ret = v4l2_async_nf_register(&priv->notifier);
  649. if (ret) {
  650. dev_err(dev, "Failed to register subdev_notifier");
  651. v4l2_async_nf_cleanup(&priv->notifier);
  652. return ret;
  653. }
  654. return 0;
  655. }
  656. static void max9286_v4l2_notifier_unregister(struct max9286_priv *priv)
  657. {
  658. if (!priv->nsources)
  659. return;
  660. v4l2_async_nf_unregister(&priv->notifier);
  661. v4l2_async_nf_cleanup(&priv->notifier);
  662. }
  663. static int max9286_s_stream(struct v4l2_subdev *sd, int enable)
  664. {
  665. struct max9286_priv *priv = sd_to_max9286(sd);
  666. struct v4l2_subdev_state *state;
  667. struct max9286_source *source;
  668. unsigned int i;
  669. bool sync = false;
  670. int ret = 0;
  671. state = v4l2_subdev_lock_and_get_active_state(sd);
  672. if (enable) {
  673. const struct v4l2_mbus_framefmt *format;
  674. /*
  675. * Get the format from the source pad, as all formats must be
  676. * identical.
  677. */
  678. format = v4l2_subdev_state_get_format(state, MAX9286_SRC_PAD);
  679. max9286_set_video_format(priv, format);
  680. max9286_set_fsync_period(priv, state);
  681. /*
  682. * The frame sync between cameras is transmitted across the
  683. * reverse channel as GPIO. We must open all channels while
  684. * streaming to allow this synchronisation signal to be shared.
  685. */
  686. max9286_i2c_mux_open(priv);
  687. /* Start all cameras. */
  688. for_each_source(priv, source) {
  689. ret = v4l2_subdev_call(source->sd, video, s_stream, 1);
  690. if (ret)
  691. goto unlock;
  692. }
  693. ret = max9286_check_video_links(priv);
  694. if (ret)
  695. goto unlock;
  696. /*
  697. * Wait until frame synchronization is locked.
  698. *
  699. * Manual says frame sync locking should take ~6 VTS.
  700. * From practical experience at least 8 are required. Give
  701. * 12 complete frames time (~400ms at 30 fps) to achieve frame
  702. * locking before returning error.
  703. */
  704. for (i = 0; i < 40; i++) {
  705. if (max9286_read(priv, 0x31) & MAX9286_FSYNC_LOCKED) {
  706. sync = true;
  707. break;
  708. }
  709. usleep_range(9000, 11000);
  710. }
  711. if (!sync) {
  712. dev_err(&priv->client->dev,
  713. "Failed to get frame synchronization\n");
  714. ret = -EXDEV; /* Invalid cross-device link */
  715. goto unlock;
  716. }
  717. /*
  718. * Configure the CSI-2 output to line interleaved mode (W x (N
  719. * x H), as opposed to the (N x W) x H mode that outputs the
  720. * images stitched side-by-side) and enable it.
  721. */
  722. max9286_write(priv, 0x15, MAX9286_CSI_IMAGE_TYP | MAX9286_VCTYPE |
  723. MAX9286_CSIOUTEN | MAX9286_EN_CCBSYB_CLK_STR |
  724. MAX9286_EN_GPI_CCBSYB);
  725. } else {
  726. max9286_write(priv, 0x15, MAX9286_VCTYPE |
  727. MAX9286_EN_CCBSYB_CLK_STR |
  728. MAX9286_EN_GPI_CCBSYB);
  729. /* Stop all cameras. */
  730. for_each_source(priv, source)
  731. v4l2_subdev_call(source->sd, video, s_stream, 0);
  732. max9286_i2c_mux_close(priv);
  733. }
  734. unlock:
  735. v4l2_subdev_unlock_state(state);
  736. return ret;
  737. }
  738. static int max9286_get_frame_interval(struct v4l2_subdev *sd,
  739. struct v4l2_subdev_state *sd_state,
  740. struct v4l2_subdev_frame_interval *interval)
  741. {
  742. if (interval->pad != MAX9286_SRC_PAD)
  743. return -EINVAL;
  744. interval->interval = *v4l2_subdev_state_get_interval(sd_state,
  745. interval->pad);
  746. return 0;
  747. }
  748. static int max9286_set_frame_interval(struct v4l2_subdev *sd,
  749. struct v4l2_subdev_state *sd_state,
  750. struct v4l2_subdev_frame_interval *interval)
  751. {
  752. if (interval->pad != MAX9286_SRC_PAD)
  753. return -EINVAL;
  754. *v4l2_subdev_state_get_interval(sd_state,
  755. interval->pad) = interval->interval;
  756. return 0;
  757. }
  758. static int max9286_enum_mbus_code(struct v4l2_subdev *sd,
  759. struct v4l2_subdev_state *sd_state,
  760. struct v4l2_subdev_mbus_code_enum *code)
  761. {
  762. if (code->pad || code->index >= ARRAY_SIZE(max9286_formats))
  763. return -EINVAL;
  764. code->code = max9286_formats[code->index].code;
  765. return 0;
  766. }
  767. static int max9286_set_fmt(struct v4l2_subdev *sd,
  768. struct v4l2_subdev_state *state,
  769. struct v4l2_subdev_format *format)
  770. {
  771. struct max9286_priv *priv = sd_to_max9286(sd);
  772. struct max9286_source *source;
  773. unsigned int i;
  774. /*
  775. * Disable setting format on the source pad: format is propagated
  776. * from the sinks.
  777. */
  778. if (format->pad == MAX9286_SRC_PAD)
  779. return v4l2_subdev_get_fmt(sd, state, format);
  780. /* Validate the format. */
  781. for (i = 0; i < ARRAY_SIZE(max9286_formats); ++i) {
  782. if (max9286_formats[i].code == format->format.code)
  783. break;
  784. }
  785. if (i == ARRAY_SIZE(max9286_formats))
  786. format->format.code = max9286_formats[0].code;
  787. /*
  788. * Apply the same format on all the other pad as all links must have the
  789. * same format.
  790. */
  791. for_each_source(priv, source) {
  792. unsigned int index = to_index(priv, source);
  793. *v4l2_subdev_state_get_format(state, index) = format->format;
  794. }
  795. *v4l2_subdev_state_get_format(state, MAX9286_SRC_PAD) = format->format;
  796. return 0;
  797. }
  798. static const struct v4l2_subdev_video_ops max9286_video_ops = {
  799. .s_stream = max9286_s_stream,
  800. };
  801. static const struct v4l2_subdev_pad_ops max9286_pad_ops = {
  802. .enum_mbus_code = max9286_enum_mbus_code,
  803. .get_fmt = v4l2_subdev_get_fmt,
  804. .set_fmt = max9286_set_fmt,
  805. .get_frame_interval = max9286_get_frame_interval,
  806. .set_frame_interval = max9286_set_frame_interval,
  807. };
  808. static const struct v4l2_subdev_ops max9286_subdev_ops = {
  809. .video = &max9286_video_ops,
  810. .pad = &max9286_pad_ops,
  811. };
  812. static const struct v4l2_mbus_framefmt max9286_default_format = {
  813. .width = 1280,
  814. .height = 800,
  815. .code = MEDIA_BUS_FMT_UYVY8_1X16,
  816. .colorspace = V4L2_COLORSPACE_SRGB,
  817. .field = V4L2_FIELD_NONE,
  818. .ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT,
  819. .quantization = V4L2_QUANTIZATION_DEFAULT,
  820. .xfer_func = V4L2_XFER_FUNC_DEFAULT,
  821. };
  822. static int max9286_init_state(struct v4l2_subdev *sd,
  823. struct v4l2_subdev_state *state)
  824. {
  825. struct v4l2_fract *interval;
  826. for (unsigned int i = 0; i < MAX9286_N_PADS; i++)
  827. *v4l2_subdev_state_get_format(state, i) = max9286_default_format;
  828. /*
  829. * Special case: a null interval enables automatic FRAMESYNC mode.
  830. *
  831. * FRAMESYNC is taken from the slowest link. See register 0x01
  832. * configuration.
  833. */
  834. interval = v4l2_subdev_state_get_interval(state, MAX9286_SRC_PAD);
  835. interval->numerator = 0;
  836. interval->denominator = 0;
  837. return 0;
  838. }
  839. static const struct v4l2_subdev_internal_ops max9286_subdev_internal_ops = {
  840. .init_state = max9286_init_state,
  841. };
  842. static const struct media_entity_operations max9286_media_ops = {
  843. .link_validate = v4l2_subdev_link_validate
  844. };
  845. static int max9286_s_ctrl(struct v4l2_ctrl *ctrl)
  846. {
  847. switch (ctrl->id) {
  848. case V4L2_CID_PIXEL_RATE:
  849. return 0;
  850. default:
  851. return -EINVAL;
  852. }
  853. }
  854. static const struct v4l2_ctrl_ops max9286_ctrl_ops = {
  855. .s_ctrl = max9286_s_ctrl,
  856. };
  857. static int max9286_v4l2_register(struct max9286_priv *priv)
  858. {
  859. struct device *dev = &priv->client->dev;
  860. int ret;
  861. int i;
  862. /* Register v4l2 async notifiers for connected Camera subdevices */
  863. ret = max9286_v4l2_notifier_register(priv);
  864. if (ret) {
  865. dev_err(dev, "Unable to register V4L2 async notifiers\n");
  866. return ret;
  867. }
  868. /* Configure V4L2 for the MAX9286 itself */
  869. v4l2_i2c_subdev_init(&priv->sd, priv->client, &max9286_subdev_ops);
  870. priv->sd.internal_ops = &max9286_subdev_internal_ops;
  871. priv->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  872. v4l2_ctrl_handler_init(&priv->ctrls, 1);
  873. priv->pixelrate_ctrl = v4l2_ctrl_new_std(&priv->ctrls,
  874. &max9286_ctrl_ops,
  875. V4L2_CID_PIXEL_RATE,
  876. 1, INT_MAX, 1, 50000000);
  877. priv->sd.ctrl_handler = &priv->ctrls;
  878. ret = priv->ctrls.error;
  879. if (ret)
  880. goto err_async;
  881. priv->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
  882. priv->sd.entity.ops = &max9286_media_ops;
  883. priv->pads[MAX9286_SRC_PAD].flags = MEDIA_PAD_FL_SOURCE;
  884. for (i = 0; i < MAX9286_SRC_PAD; i++)
  885. priv->pads[i].flags = MEDIA_PAD_FL_SINK;
  886. ret = media_entity_pads_init(&priv->sd.entity, MAX9286_N_PADS,
  887. priv->pads);
  888. if (ret)
  889. goto err_async;
  890. priv->sd.state_lock = priv->ctrls.lock;
  891. ret = v4l2_subdev_init_finalize(&priv->sd);
  892. if (ret)
  893. goto err_async;
  894. ret = v4l2_async_register_subdev(&priv->sd);
  895. if (ret < 0) {
  896. dev_err(dev, "Unable to register subdevice\n");
  897. goto err_subdev;
  898. }
  899. return 0;
  900. err_subdev:
  901. v4l2_subdev_cleanup(&priv->sd);
  902. err_async:
  903. v4l2_ctrl_handler_free(&priv->ctrls);
  904. max9286_v4l2_notifier_unregister(priv);
  905. return ret;
  906. }
  907. static void max9286_v4l2_unregister(struct max9286_priv *priv)
  908. {
  909. v4l2_subdev_cleanup(&priv->sd);
  910. v4l2_ctrl_handler_free(&priv->ctrls);
  911. v4l2_async_unregister_subdev(&priv->sd);
  912. max9286_v4l2_notifier_unregister(priv);
  913. }
  914. /* -----------------------------------------------------------------------------
  915. * Probe/Remove
  916. */
  917. static int max9286_setup(struct max9286_priv *priv)
  918. {
  919. /*
  920. * Link ordering values for all enabled links combinations. Orders must
  921. * be assigned sequentially from 0 to the number of enabled links
  922. * without leaving any hole for disabled links. We thus assign orders to
  923. * enabled links first, and use the remaining order values for disabled
  924. * links are all links must have a different order value;
  925. */
  926. static const u8 link_order[] = {
  927. (3 << 6) | (2 << 4) | (1 << 2) | (0 << 0), /* xxxx */
  928. (3 << 6) | (2 << 4) | (1 << 2) | (0 << 0), /* xxx0 */
  929. (3 << 6) | (2 << 4) | (0 << 2) | (1 << 0), /* xx0x */
  930. (3 << 6) | (2 << 4) | (1 << 2) | (0 << 0), /* xx10 */
  931. (3 << 6) | (0 << 4) | (2 << 2) | (1 << 0), /* x0xx */
  932. (3 << 6) | (1 << 4) | (2 << 2) | (0 << 0), /* x1x0 */
  933. (3 << 6) | (1 << 4) | (0 << 2) | (2 << 0), /* x10x */
  934. (3 << 6) | (1 << 4) | (1 << 2) | (0 << 0), /* x210 */
  935. (0 << 6) | (3 << 4) | (2 << 2) | (1 << 0), /* 0xxx */
  936. (1 << 6) | (3 << 4) | (2 << 2) | (0 << 0), /* 1xx0 */
  937. (1 << 6) | (3 << 4) | (0 << 2) | (2 << 0), /* 1x0x */
  938. (2 << 6) | (3 << 4) | (1 << 2) | (0 << 0), /* 2x10 */
  939. (1 << 6) | (0 << 4) | (3 << 2) | (2 << 0), /* 10xx */
  940. (2 << 6) | (1 << 4) | (3 << 2) | (0 << 0), /* 21x0 */
  941. (2 << 6) | (1 << 4) | (0 << 2) | (3 << 0), /* 210x */
  942. (3 << 6) | (2 << 4) | (1 << 2) | (0 << 0), /* 3210 */
  943. };
  944. int cfg;
  945. /*
  946. * Set the I2C bus speed.
  947. *
  948. * Enable I2C Local Acknowledge during the probe sequences of the camera
  949. * only. This should be disabled after the mux is initialised.
  950. */
  951. max9286_configure_i2c(priv, true);
  952. max9286_reverse_channel_setup(priv, priv->init_rev_chan_mv);
  953. /*
  954. * Enable GMSL links, mask unused ones and autodetect link
  955. * used as CSI clock source.
  956. */
  957. max9286_write(priv, 0x00, MAX9286_MSTLINKSEL_AUTO | priv->route_mask);
  958. max9286_write(priv, 0x0b, link_order[priv->route_mask]);
  959. max9286_write(priv, 0x69, (0xf & ~priv->route_mask));
  960. max9286_set_video_format(priv, &max9286_default_format);
  961. cfg = max9286_read(priv, 0x1c);
  962. if (cfg < 0)
  963. return cfg;
  964. dev_dbg(&priv->client->dev, "power-up config: %s immunity, %u-bit bus\n",
  965. cfg & MAX9286_HIGHIMM(0) ? "high" : "legacy",
  966. cfg & MAX9286_BWS ? 32 : cfg & MAX9286_HIBW ? 27 : 24);
  967. if (priv->bus_width) {
  968. cfg &= ~(MAX9286_HIBW | MAX9286_BWS);
  969. if (priv->bus_width == 27)
  970. cfg |= MAX9286_HIBW;
  971. else if (priv->bus_width == 32)
  972. cfg |= MAX9286_BWS;
  973. max9286_write(priv, 0x1c, cfg);
  974. }
  975. /*
  976. * The overlap window seems to provide additional validation by tracking
  977. * the delay between vsync and frame sync, generating an error if the
  978. * delay is bigger than the programmed window, though it's not yet clear
  979. * what value should be set.
  980. *
  981. * As it's an optional value and can be disabled, we do so by setting
  982. * a 0 overlap value.
  983. */
  984. max9286_write(priv, 0x63, 0);
  985. max9286_write(priv, 0x64, 0);
  986. /*
  987. * Wait for 2ms to allow the link to resynchronize after the
  988. * configuration change.
  989. */
  990. usleep_range(2000, 5000);
  991. return 0;
  992. }
  993. static int max9286_gpio_set(struct max9286_priv *priv, unsigned int offset,
  994. int value)
  995. {
  996. if (value)
  997. priv->gpio_state |= BIT(offset);
  998. else
  999. priv->gpio_state &= ~BIT(offset);
  1000. return max9286_write(priv, 0x0f,
  1001. MAX9286_0X0F_RESERVED | priv->gpio_state);
  1002. }
  1003. static int max9286_gpiochip_set(struct gpio_chip *chip,
  1004. unsigned int offset, int value)
  1005. {
  1006. struct max9286_priv *priv = gpiochip_get_data(chip);
  1007. return max9286_gpio_set(priv, offset, value);
  1008. }
  1009. static int max9286_gpiochip_get(struct gpio_chip *chip, unsigned int offset)
  1010. {
  1011. struct max9286_priv *priv = gpiochip_get_data(chip);
  1012. return priv->gpio_state & BIT(offset);
  1013. }
  1014. static int max9286_register_gpio(struct max9286_priv *priv)
  1015. {
  1016. struct device *dev = &priv->client->dev;
  1017. struct gpio_chip *gpio = &priv->gpio;
  1018. int ret;
  1019. /* Configure the GPIO */
  1020. gpio->label = dev_name(dev);
  1021. gpio->parent = dev;
  1022. gpio->owner = THIS_MODULE;
  1023. gpio->ngpio = 2;
  1024. gpio->base = -1;
  1025. gpio->set = max9286_gpiochip_set;
  1026. gpio->get = max9286_gpiochip_get;
  1027. gpio->can_sleep = true;
  1028. ret = devm_gpiochip_add_data(dev, gpio, priv);
  1029. if (ret)
  1030. dev_err(dev, "Unable to create gpio_chip\n");
  1031. return ret;
  1032. }
  1033. static int max9286_parse_gpios(struct max9286_priv *priv)
  1034. {
  1035. struct device *dev = &priv->client->dev;
  1036. int ret;
  1037. /*
  1038. * Parse the "gpio-poc" vendor property. If the property is not
  1039. * specified the camera power is controlled by a regulator.
  1040. */
  1041. ret = of_property_read_u32_array(dev->of_node, "maxim,gpio-poc",
  1042. priv->gpio_poc, 2);
  1043. if (ret == -EINVAL) {
  1044. /*
  1045. * If gpio lines are not used for the camera power, register
  1046. * a gpio controller for consumers.
  1047. */
  1048. return max9286_register_gpio(priv);
  1049. }
  1050. /* If the property is specified make sure it is well formed. */
  1051. if (ret || priv->gpio_poc[0] > 1 ||
  1052. (priv->gpio_poc[1] != GPIO_ACTIVE_HIGH &&
  1053. priv->gpio_poc[1] != GPIO_ACTIVE_LOW)) {
  1054. dev_err(dev, "Invalid 'gpio-poc' property\n");
  1055. return -EINVAL;
  1056. }
  1057. priv->use_gpio_poc = true;
  1058. return 0;
  1059. }
  1060. static int max9286_poc_power_on(struct max9286_priv *priv)
  1061. {
  1062. struct max9286_source *source;
  1063. unsigned int enabled = 0;
  1064. int ret;
  1065. /* Enable the global regulator if available. */
  1066. if (priv->regulator)
  1067. return regulator_enable(priv->regulator);
  1068. if (priv->use_gpio_poc)
  1069. return max9286_gpio_set(priv, priv->gpio_poc[0],
  1070. !priv->gpio_poc[1]);
  1071. /* Otherwise use the per-port regulators. */
  1072. for_each_source(priv, source) {
  1073. ret = regulator_enable(source->regulator);
  1074. if (ret < 0)
  1075. goto error;
  1076. enabled |= BIT(to_index(priv, source));
  1077. }
  1078. return 0;
  1079. error:
  1080. for_each_source(priv, source) {
  1081. if (enabled & BIT(to_index(priv, source)))
  1082. regulator_disable(source->regulator);
  1083. }
  1084. return ret;
  1085. }
  1086. static int max9286_poc_power_off(struct max9286_priv *priv)
  1087. {
  1088. struct max9286_source *source;
  1089. int ret = 0;
  1090. if (priv->regulator)
  1091. return regulator_disable(priv->regulator);
  1092. if (priv->use_gpio_poc)
  1093. return max9286_gpio_set(priv, priv->gpio_poc[0],
  1094. priv->gpio_poc[1]);
  1095. for_each_source(priv, source) {
  1096. int err;
  1097. err = regulator_disable(source->regulator);
  1098. if (!ret)
  1099. ret = err;
  1100. }
  1101. return ret;
  1102. }
  1103. static int max9286_poc_enable(struct max9286_priv *priv, bool enable)
  1104. {
  1105. int ret;
  1106. if (enable)
  1107. ret = max9286_poc_power_on(priv);
  1108. else
  1109. ret = max9286_poc_power_off(priv);
  1110. if (ret < 0)
  1111. dev_err(&priv->client->dev, "Unable to turn power %s\n",
  1112. enable ? "on" : "off");
  1113. return ret;
  1114. }
  1115. static int max9286_init(struct max9286_priv *priv)
  1116. {
  1117. struct i2c_client *client = priv->client;
  1118. int ret;
  1119. ret = max9286_poc_enable(priv, true);
  1120. if (ret)
  1121. return ret;
  1122. ret = max9286_setup(priv);
  1123. if (ret) {
  1124. dev_err(&client->dev, "Unable to setup max9286\n");
  1125. goto err_poc_disable;
  1126. }
  1127. /*
  1128. * Register all V4L2 interactions for the MAX9286 and notifiers for
  1129. * any subdevices connected.
  1130. */
  1131. ret = max9286_v4l2_register(priv);
  1132. if (ret) {
  1133. dev_err(&client->dev, "Failed to register with V4L2\n");
  1134. goto err_poc_disable;
  1135. }
  1136. ret = max9286_i2c_mux_init(priv);
  1137. if (ret) {
  1138. dev_err(&client->dev, "Unable to initialize I2C multiplexer\n");
  1139. goto err_v4l2_register;
  1140. }
  1141. /* Leave the mux channels disabled until they are selected. */
  1142. max9286_i2c_mux_close(priv);
  1143. return 0;
  1144. err_v4l2_register:
  1145. max9286_v4l2_unregister(priv);
  1146. err_poc_disable:
  1147. max9286_poc_enable(priv, false);
  1148. return ret;
  1149. }
  1150. static void max9286_cleanup_dt(struct max9286_priv *priv)
  1151. {
  1152. struct max9286_source *source;
  1153. for_each_source(priv, source) {
  1154. fwnode_handle_put(source->fwnode);
  1155. source->fwnode = NULL;
  1156. }
  1157. }
  1158. static int max9286_parse_dt(struct max9286_priv *priv)
  1159. {
  1160. struct device *dev = &priv->client->dev;
  1161. struct device_node *i2c_mux;
  1162. struct device_node *node = NULL;
  1163. unsigned int i2c_mux_mask = 0;
  1164. u32 reverse_channel_microvolt;
  1165. u32 i2c_clk_freq = 105000;
  1166. unsigned int i;
  1167. /* Balance the of_node_put() performed by of_find_node_by_name(). */
  1168. of_node_get(dev->of_node);
  1169. i2c_mux = of_find_node_by_name(dev->of_node, "i2c-mux");
  1170. if (!i2c_mux) {
  1171. dev_err(dev, "Failed to find i2c-mux node\n");
  1172. return -EINVAL;
  1173. }
  1174. /* Identify which i2c-mux channels are enabled */
  1175. for_each_child_of_node(i2c_mux, node) {
  1176. u32 id = 0;
  1177. of_property_read_u32(node, "reg", &id);
  1178. if (id >= MAX9286_NUM_GMSL)
  1179. continue;
  1180. if (!of_device_is_available(node)) {
  1181. dev_dbg(dev, "Skipping disabled I2C bus port %u\n", id);
  1182. continue;
  1183. }
  1184. i2c_mux_mask |= BIT(id);
  1185. }
  1186. of_node_put(i2c_mux);
  1187. /* Parse the endpoints */
  1188. for_each_endpoint_of_node(dev->of_node, node) {
  1189. struct max9286_source *source;
  1190. struct of_endpoint ep;
  1191. of_graph_parse_endpoint(node, &ep);
  1192. dev_dbg(dev, "Endpoint %pOF on port %d",
  1193. ep.local_node, ep.port);
  1194. if (ep.port > MAX9286_NUM_GMSL) {
  1195. dev_err(dev, "Invalid endpoint %s on port %d",
  1196. of_node_full_name(ep.local_node), ep.port);
  1197. continue;
  1198. }
  1199. /* For the source endpoint just parse the bus configuration. */
  1200. if (ep.port == MAX9286_SRC_PAD) {
  1201. struct v4l2_fwnode_endpoint vep = {
  1202. .bus_type = V4L2_MBUS_CSI2_DPHY
  1203. };
  1204. int ret;
  1205. ret = v4l2_fwnode_endpoint_parse(
  1206. of_fwnode_handle(node), &vep);
  1207. if (ret) {
  1208. of_node_put(node);
  1209. return ret;
  1210. }
  1211. priv->csi2_data_lanes =
  1212. vep.bus.mipi_csi2.num_data_lanes;
  1213. continue;
  1214. }
  1215. /* Skip if the corresponding GMSL link is unavailable. */
  1216. if (!(i2c_mux_mask & BIT(ep.port)))
  1217. continue;
  1218. if (priv->sources[ep.port].fwnode) {
  1219. dev_err(dev,
  1220. "Multiple port endpoints are not supported: %d",
  1221. ep.port);
  1222. continue;
  1223. }
  1224. source = &priv->sources[ep.port];
  1225. source->fwnode = fwnode_graph_get_remote_endpoint(
  1226. of_fwnode_handle(node));
  1227. if (!source->fwnode) {
  1228. dev_err(dev,
  1229. "Endpoint %pOF has no remote endpoint connection\n",
  1230. ep.local_node);
  1231. continue;
  1232. }
  1233. priv->source_mask |= BIT(ep.port);
  1234. priv->nsources++;
  1235. }
  1236. of_property_read_u32(dev->of_node, "maxim,bus-width", &priv->bus_width);
  1237. switch (priv->bus_width) {
  1238. case 0:
  1239. /*
  1240. * The property isn't specified in the device tree, the driver
  1241. * will keep the default value selected by the BWS pin.
  1242. */
  1243. case 24:
  1244. case 27:
  1245. case 32:
  1246. break;
  1247. default:
  1248. dev_err(dev, "Invalid %s value %u\n", "maxim,bus-width",
  1249. priv->bus_width);
  1250. return -EINVAL;
  1251. }
  1252. of_property_read_u32(dev->of_node, "maxim,i2c-remote-bus-hz",
  1253. &i2c_clk_freq);
  1254. for (i = 0; i < ARRAY_SIZE(max9286_i2c_speeds); ++i) {
  1255. const struct max9286_i2c_speed *speed = &max9286_i2c_speeds[i];
  1256. if (speed->rate == i2c_clk_freq) {
  1257. priv->i2c_mstbt = speed->mstbt;
  1258. break;
  1259. }
  1260. }
  1261. if (i == ARRAY_SIZE(max9286_i2c_speeds)) {
  1262. dev_err(dev, "Invalid %s value %u\n", "maxim,i2c-remote-bus-hz",
  1263. i2c_clk_freq);
  1264. return -EINVAL;
  1265. }
  1266. /*
  1267. * Parse the initial value of the reverse channel amplitude from
  1268. * the firmware interface and convert it to millivolts.
  1269. *
  1270. * Default it to 170mV for backward compatibility with DTBs that do not
  1271. * provide the property.
  1272. */
  1273. if (of_property_read_u32(dev->of_node,
  1274. "maxim,reverse-channel-microvolt",
  1275. &reverse_channel_microvolt))
  1276. priv->init_rev_chan_mv = 170;
  1277. else
  1278. priv->init_rev_chan_mv = reverse_channel_microvolt / 1000U;
  1279. priv->route_mask = priv->source_mask;
  1280. return 0;
  1281. }
  1282. static int max9286_get_poc_supplies(struct max9286_priv *priv)
  1283. {
  1284. struct device *dev = &priv->client->dev;
  1285. struct max9286_source *source;
  1286. int ret;
  1287. /* Start by getting the global regulator. */
  1288. priv->regulator = devm_regulator_get_optional(dev, "poc");
  1289. if (!IS_ERR(priv->regulator))
  1290. return 0;
  1291. if (PTR_ERR(priv->regulator) != -ENODEV)
  1292. return dev_err_probe(dev, PTR_ERR(priv->regulator),
  1293. "Unable to get PoC regulator\n");
  1294. /* If there's no global regulator, get per-port regulators. */
  1295. dev_dbg(dev,
  1296. "No global PoC regulator, looking for per-port regulators\n");
  1297. priv->regulator = NULL;
  1298. for_each_source(priv, source) {
  1299. unsigned int index = to_index(priv, source);
  1300. char name[10];
  1301. snprintf(name, sizeof(name), "port%u-poc", index);
  1302. source->regulator = devm_regulator_get(dev, name);
  1303. if (IS_ERR(source->regulator)) {
  1304. ret = PTR_ERR(source->regulator);
  1305. dev_err_probe(dev, ret,
  1306. "Unable to get port %u PoC regulator\n",
  1307. index);
  1308. return ret;
  1309. }
  1310. }
  1311. return 0;
  1312. }
  1313. static int max9286_probe(struct i2c_client *client)
  1314. {
  1315. struct max9286_priv *priv;
  1316. int ret;
  1317. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  1318. if (!priv)
  1319. return -ENOMEM;
  1320. priv->client = client;
  1321. /* GPIO values default to high */
  1322. priv->gpio_state = BIT(0) | BIT(1);
  1323. ret = max9286_parse_dt(priv);
  1324. if (ret)
  1325. goto err_cleanup_dt;
  1326. priv->gpiod_pwdn = devm_gpiod_get_optional(&client->dev, "enable",
  1327. GPIOD_OUT_HIGH);
  1328. if (IS_ERR(priv->gpiod_pwdn)) {
  1329. ret = PTR_ERR(priv->gpiod_pwdn);
  1330. goto err_cleanup_dt;
  1331. }
  1332. gpiod_set_consumer_name(priv->gpiod_pwdn, "max9286-pwdn");
  1333. gpiod_set_value_cansleep(priv->gpiod_pwdn, 1);
  1334. /* Wait at least 4ms before the I2C lines latch to the address */
  1335. if (priv->gpiod_pwdn)
  1336. usleep_range(4000, 5000);
  1337. /*
  1338. * The MAX9286 starts by default with all ports enabled, we disable all
  1339. * ports early to ensure that all channels are disabled if we error out
  1340. * and keep the bus consistent.
  1341. */
  1342. max9286_i2c_mux_close(priv);
  1343. /*
  1344. * The MAX9286 initialises with auto-acknowledge enabled by default.
  1345. * This can be invasive to other transactions on the same bus, so
  1346. * disable it early. It will be enabled only as and when needed.
  1347. */
  1348. max9286_configure_i2c(priv, false);
  1349. ret = max9286_parse_gpios(priv);
  1350. if (ret)
  1351. goto err_powerdown;
  1352. if (!priv->use_gpio_poc) {
  1353. ret = max9286_get_poc_supplies(priv);
  1354. if (ret)
  1355. goto err_cleanup_dt;
  1356. }
  1357. ret = max9286_init(priv);
  1358. if (ret < 0)
  1359. goto err_cleanup_dt;
  1360. return 0;
  1361. err_powerdown:
  1362. gpiod_set_value_cansleep(priv->gpiod_pwdn, 0);
  1363. err_cleanup_dt:
  1364. max9286_cleanup_dt(priv);
  1365. return ret;
  1366. }
  1367. static void max9286_remove(struct i2c_client *client)
  1368. {
  1369. struct max9286_priv *priv = sd_to_max9286(i2c_get_clientdata(client));
  1370. i2c_mux_del_adapters(priv->mux);
  1371. max9286_v4l2_unregister(priv);
  1372. max9286_poc_enable(priv, false);
  1373. gpiod_set_value_cansleep(priv->gpiod_pwdn, 0);
  1374. max9286_cleanup_dt(priv);
  1375. }
  1376. static const struct of_device_id max9286_dt_ids[] = {
  1377. { .compatible = "maxim,max9286" },
  1378. {},
  1379. };
  1380. MODULE_DEVICE_TABLE(of, max9286_dt_ids);
  1381. static struct i2c_driver max9286_i2c_driver = {
  1382. .driver = {
  1383. .name = "max9286",
  1384. .of_match_table = max9286_dt_ids,
  1385. },
  1386. .probe = max9286_probe,
  1387. .remove = max9286_remove,
  1388. };
  1389. module_i2c_driver(max9286_i2c_driver);
  1390. MODULE_DESCRIPTION("Maxim MAX9286 GMSL Deserializer Driver");
  1391. MODULE_AUTHOR("Jacopo Mondi, Kieran Bingham, Laurent Pinchart, Niklas Söderlund, Vladimir Barinov");
  1392. MODULE_LICENSE("GPL");