max96714.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Maxim GMSL2 Deserializer Driver
  4. *
  5. * Copyright (C) 2024 Collabora Ltd.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/bitops.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/i2c.h>
  11. #include <linux/i2c-mux.h>
  12. #include <linux/module.h>
  13. #include <linux/property.h>
  14. #include <linux/regmap.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <media/v4l2-cci.h>
  17. #include <media/v4l2-ctrls.h>
  18. #include <media/v4l2-fwnode.h>
  19. #include <media/v4l2-subdev.h>
  20. #define MAX96714_DEVICE_ID 0xc9
  21. #define MAX96714F_DEVICE_ID 0xca
  22. #define MAX96714_NPORTS 2
  23. #define MAX96714_PAD_SINK 0
  24. #define MAX96714_PAD_SOURCE 1
  25. #define MAX96714_CSI_NLANES 4
  26. /* DEV */
  27. #define MAX96714_REG13 CCI_REG8(0x0d)
  28. #define MAX96714_DEV_REV CCI_REG8(0x0e)
  29. #define MAX96714_DEV_REV_MASK GENMASK(3, 0)
  30. #define MAX96714_LINK_LOCK CCI_REG8(0x13)
  31. #define MAX96714_LINK_LOCK_BIT BIT(3)
  32. #define MAX96714_IO_CHK0 CCI_REG8(0x38)
  33. #define MAX96714_PATTERN_CLK_FREQ GENMASK(1, 0)
  34. /* VID_RX */
  35. #define MAX96714_VIDEO_RX8 CCI_REG8(0x11a)
  36. #define MAX96714_VID_LOCK BIT(6)
  37. /* VRX_PATGEN_0 */
  38. #define MAX96714_PATGEN_0 CCI_REG8(0x240)
  39. #define MAX96714_PATGEN_1 CCI_REG8(0x241)
  40. #define MAX96714_PATGEN_MODE GENMASK(5, 4)
  41. #define MAX96714_PATGEN_VS_DLY CCI_REG24(0x242)
  42. #define MAX96714_PATGEN_VS_HIGH CCI_REG24(0x245)
  43. #define MAX96714_PATGEN_VS_LOW CCI_REG24(0x248)
  44. #define MAX96714_PATGEN_V2H CCI_REG24(0x24b)
  45. #define MAX96714_PATGEN_HS_HIGH CCI_REG16(0x24e)
  46. #define MAX96714_PATGEN_HS_LOW CCI_REG16(0x250)
  47. #define MAX96714_PATGEN_HS_CNT CCI_REG16(0x252)
  48. #define MAX96714_PATGEN_V2D CCI_REG24(0x254)
  49. #define MAX96714_PATGEN_DE_HIGH CCI_REG16(0x257)
  50. #define MAX96714_PATGEN_DE_LOW CCI_REG16(0x259)
  51. #define MAX96714_PATGEN_DE_CNT CCI_REG16(0x25b)
  52. #define MAX96714_PATGEN_GRAD_INC CCI_REG8(0x25d)
  53. #define MAX96714_PATGEN_CHKB_COLOR_A CCI_REG24(0x25e)
  54. #define MAX96714_PATGEN_CHKB_COLOR_B CCI_REG24(0x261)
  55. #define MAX96714_PATGEN_CHKB_RPT_CNT_A CCI_REG8(0x264)
  56. #define MAX96714_PATGEN_CHKB_RPT_CNT_B CCI_REG8(0x265)
  57. #define MAX96714_PATGEN_CHKB_ALT CCI_REG8(0x266)
  58. /* BACKTOP */
  59. #define MAX96714_BACKTOP25 CCI_REG8(0x320)
  60. #define CSI_DPLL_FREQ_MASK GENMASK(4, 0)
  61. /* MIPI_PHY */
  62. #define MAX96714_MIPI_PHY0 CCI_REG8(0x330)
  63. #define MAX96714_FORCE_CSI_OUT BIT(7)
  64. #define MAX96714_MIPI_STDBY_N CCI_REG8(0x332)
  65. #define MAX96714_MIPI_STDBY_MASK GENMASK(5, 4)
  66. #define MAX96714_MIPI_LANE_MAP CCI_REG8(0x333)
  67. #define MAX96714_MIPI_POLARITY CCI_REG8(0x335)
  68. #define MAX96714_MIPI_POLARITY_MASK GENMASK(5, 0)
  69. /* MIPI_TX */
  70. #define MAX96714_MIPI_LANE_CNT CCI_REG8(0x44a)
  71. #define MAX96714_CSI2_LANE_CNT_MASK GENMASK(7, 6)
  72. #define MAX96714_MIPI_TX52 CCI_REG8(0x474)
  73. #define MAX96714_TUN_EN BIT(0)
  74. #define MHZ(v) ((u32)((v) * 1000000U))
  75. enum max96714_vpg_mode {
  76. MAX96714_VPG_DISABLED = 0,
  77. MAX96714_VPG_CHECKERBOARD = 1,
  78. MAX96714_VPG_GRADIENT = 2,
  79. };
  80. struct max96714_rxport {
  81. struct {
  82. struct v4l2_subdev *sd;
  83. u16 pad;
  84. struct fwnode_handle *ep_fwnode;
  85. } source;
  86. struct regulator *poc;
  87. };
  88. struct max96714_txport {
  89. struct v4l2_fwnode_endpoint vep;
  90. };
  91. struct max96714_priv {
  92. struct i2c_client *client;
  93. struct regmap *regmap;
  94. struct gpio_desc *pd_gpio;
  95. struct max96714_rxport rxport;
  96. struct i2c_mux_core *mux;
  97. u64 enabled_source_streams;
  98. struct v4l2_subdev sd;
  99. struct media_pad pads[MAX96714_NPORTS];
  100. struct v4l2_mbus_config_mipi_csi2 mipi_csi2;
  101. struct v4l2_ctrl_handler ctrl_handler;
  102. struct v4l2_async_notifier notifier;
  103. s64 tx_link_freq;
  104. enum max96714_vpg_mode pattern;
  105. };
  106. static inline struct max96714_priv *sd_to_max96714(struct v4l2_subdev *sd)
  107. {
  108. return container_of(sd, struct max96714_priv, sd);
  109. }
  110. static int max96714_enable_tx_port(struct max96714_priv *priv)
  111. {
  112. return cci_update_bits(priv->regmap, MAX96714_MIPI_STDBY_N,
  113. MAX96714_MIPI_STDBY_MASK,
  114. MAX96714_MIPI_STDBY_MASK, NULL);
  115. }
  116. static int max96714_disable_tx_port(struct max96714_priv *priv)
  117. {
  118. return cci_update_bits(priv->regmap, MAX96714_MIPI_STDBY_N,
  119. MAX96714_MIPI_STDBY_MASK, 0, NULL);
  120. }
  121. static bool max96714_tx_port_enabled(struct max96714_priv *priv)
  122. {
  123. u64 val;
  124. cci_read(priv->regmap, MAX96714_MIPI_STDBY_N, &val, NULL);
  125. return val & MAX96714_MIPI_STDBY_MASK;
  126. }
  127. static int max96714_apply_patgen_timing(struct max96714_priv *priv,
  128. struct v4l2_subdev_state *state)
  129. {
  130. struct v4l2_mbus_framefmt *fmt =
  131. v4l2_subdev_state_get_format(state, MAX96714_PAD_SOURCE);
  132. const u32 h_active = fmt->width;
  133. const u32 h_fp = 88;
  134. const u32 h_sw = 44;
  135. const u32 h_bp = 148;
  136. u32 h_tot;
  137. const u32 v_active = fmt->height;
  138. const u32 v_fp = 4;
  139. const u32 v_sw = 5;
  140. const u32 v_bp = 36;
  141. u32 v_tot;
  142. int ret = 0;
  143. h_tot = h_active + h_fp + h_sw + h_bp;
  144. v_tot = v_active + v_fp + v_sw + v_bp;
  145. /* 75 Mhz pixel clock */
  146. cci_update_bits(priv->regmap, MAX96714_IO_CHK0,
  147. MAX96714_PATTERN_CLK_FREQ, 1, &ret);
  148. dev_info(&priv->client->dev, "height: %d width: %d\n", fmt->height,
  149. fmt->width);
  150. cci_write(priv->regmap, MAX96714_PATGEN_VS_DLY, 0, &ret);
  151. cci_write(priv->regmap, MAX96714_PATGEN_VS_HIGH, v_sw * h_tot, &ret);
  152. cci_write(priv->regmap, MAX96714_PATGEN_VS_LOW,
  153. (v_active + v_fp + v_bp) * h_tot, &ret);
  154. cci_write(priv->regmap, MAX96714_PATGEN_HS_HIGH, h_sw, &ret);
  155. cci_write(priv->regmap, MAX96714_PATGEN_HS_LOW, h_active + h_fp + h_bp,
  156. &ret);
  157. cci_write(priv->regmap, MAX96714_PATGEN_V2D,
  158. h_tot * (v_sw + v_bp) + (h_sw + h_bp), &ret);
  159. cci_write(priv->regmap, MAX96714_PATGEN_HS_CNT, v_tot, &ret);
  160. cci_write(priv->regmap, MAX96714_PATGEN_DE_HIGH, h_active, &ret);
  161. cci_write(priv->regmap, MAX96714_PATGEN_DE_LOW, h_fp + h_sw + h_bp,
  162. &ret);
  163. cci_write(priv->regmap, MAX96714_PATGEN_DE_CNT, v_active, &ret);
  164. /* B G R */
  165. cci_write(priv->regmap, MAX96714_PATGEN_CHKB_COLOR_A, 0xfecc00, &ret);
  166. /* B G R */
  167. cci_write(priv->regmap, MAX96714_PATGEN_CHKB_COLOR_B, 0x006aa7, &ret);
  168. cci_write(priv->regmap, MAX96714_PATGEN_CHKB_RPT_CNT_A, 0x3c, &ret);
  169. cci_write(priv->regmap, MAX96714_PATGEN_CHKB_RPT_CNT_B, 0x3c, &ret);
  170. cci_write(priv->regmap, MAX96714_PATGEN_CHKB_ALT, 0x3c, &ret);
  171. cci_write(priv->regmap, MAX96714_PATGEN_GRAD_INC, 0x10, &ret);
  172. return ret;
  173. }
  174. static int max96714_apply_patgen(struct max96714_priv *priv,
  175. struct v4l2_subdev_state *state)
  176. {
  177. unsigned int val;
  178. int ret = 0;
  179. if (priv->pattern)
  180. ret = max96714_apply_patgen_timing(priv, state);
  181. cci_write(priv->regmap, MAX96714_PATGEN_0, priv->pattern ? 0xfb : 0,
  182. &ret);
  183. val = FIELD_PREP(MAX96714_PATGEN_MODE, priv->pattern);
  184. cci_update_bits(priv->regmap, MAX96714_PATGEN_1, MAX96714_PATGEN_MODE,
  185. val, &ret);
  186. return ret;
  187. }
  188. static int max96714_s_ctrl(struct v4l2_ctrl *ctrl)
  189. {
  190. struct max96714_priv *priv =
  191. container_of(ctrl->handler, struct max96714_priv, ctrl_handler);
  192. int ret;
  193. switch (ctrl->id) {
  194. case V4L2_CID_TEST_PATTERN:
  195. if (priv->enabled_source_streams)
  196. return -EBUSY;
  197. priv->pattern = ctrl->val;
  198. break;
  199. default:
  200. return -EINVAL;
  201. }
  202. ret = cci_update_bits(priv->regmap, MAX96714_MIPI_PHY0,
  203. MAX96714_FORCE_CSI_OUT,
  204. priv->pattern ? MAX96714_FORCE_CSI_OUT : 0, NULL);
  205. /* Pattern generator doesn't work with tunnel mode */
  206. return cci_update_bits(priv->regmap, MAX96714_MIPI_TX52,
  207. MAX96714_TUN_EN,
  208. priv->pattern ? 0 : MAX96714_TUN_EN, &ret);
  209. }
  210. static const char * const max96714_test_pattern[] = {
  211. "Disabled",
  212. "Checkerboard",
  213. "Gradient"
  214. };
  215. static const struct v4l2_ctrl_ops max96714_ctrl_ops = {
  216. .s_ctrl = max96714_s_ctrl,
  217. };
  218. static int max96714_enable_streams(struct v4l2_subdev *sd,
  219. struct v4l2_subdev_state *state,
  220. u32 source_pad, u64 streams_mask)
  221. {
  222. struct max96714_priv *priv = sd_to_max96714(sd);
  223. u64 sink_streams;
  224. int ret;
  225. if (!priv->enabled_source_streams)
  226. max96714_enable_tx_port(priv);
  227. ret = max96714_apply_patgen(priv, state);
  228. if (ret)
  229. goto err;
  230. if (!priv->pattern) {
  231. if (!priv->rxport.source.sd) {
  232. ret = -ENODEV;
  233. goto err;
  234. }
  235. sink_streams =
  236. v4l2_subdev_state_xlate_streams(state,
  237. MAX96714_PAD_SOURCE,
  238. MAX96714_PAD_SINK,
  239. &streams_mask);
  240. ret = v4l2_subdev_enable_streams(priv->rxport.source.sd,
  241. priv->rxport.source.pad,
  242. sink_streams);
  243. if (ret)
  244. goto err;
  245. }
  246. priv->enabled_source_streams |= streams_mask;
  247. return 0;
  248. err:
  249. if (!priv->enabled_source_streams)
  250. max96714_disable_tx_port(priv);
  251. return ret;
  252. }
  253. static int max96714_disable_streams(struct v4l2_subdev *sd,
  254. struct v4l2_subdev_state *state,
  255. u32 source_pad, u64 streams_mask)
  256. {
  257. struct max96714_priv *priv = sd_to_max96714(sd);
  258. u64 sink_streams;
  259. if (!priv->pattern) {
  260. int ret;
  261. sink_streams =
  262. v4l2_subdev_state_xlate_streams(state,
  263. MAX96714_PAD_SOURCE,
  264. MAX96714_PAD_SINK,
  265. &streams_mask);
  266. ret = v4l2_subdev_disable_streams(priv->rxport.source.sd,
  267. priv->rxport.source.pad,
  268. sink_streams);
  269. if (ret)
  270. return ret;
  271. }
  272. priv->enabled_source_streams &= ~streams_mask;
  273. if (!priv->enabled_source_streams)
  274. max96714_disable_tx_port(priv);
  275. return 0;
  276. }
  277. static int max96714_set_fmt(struct v4l2_subdev *sd,
  278. struct v4l2_subdev_state *state,
  279. struct v4l2_subdev_format *format)
  280. {
  281. struct max96714_priv *priv = sd_to_max96714(sd);
  282. struct v4l2_mbus_framefmt *fmt;
  283. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE &&
  284. priv->enabled_source_streams)
  285. return -EBUSY;
  286. /* No transcoding, source and sink formats must match. */
  287. if (format->pad == MAX96714_PAD_SOURCE)
  288. return v4l2_subdev_get_fmt(sd, state, format);
  289. fmt = v4l2_subdev_state_get_format(state, format->pad, format->stream);
  290. if (!fmt)
  291. return -EINVAL;
  292. *fmt = format->format;
  293. fmt = v4l2_subdev_state_get_opposite_stream_format(state, format->pad,
  294. format->stream);
  295. if (!fmt)
  296. return -EINVAL;
  297. *fmt = format->format;
  298. return 0;
  299. }
  300. static int _max96714_set_routing(struct v4l2_subdev *sd,
  301. struct v4l2_subdev_state *state,
  302. enum v4l2_subdev_format_whence which,
  303. struct v4l2_subdev_krouting *routing)
  304. {
  305. static const struct v4l2_mbus_framefmt format = {
  306. .width = 1280,
  307. .height = 1080,
  308. .code = MEDIA_BUS_FMT_Y8_1X8,
  309. .field = V4L2_FIELD_NONE,
  310. };
  311. int ret;
  312. ret = v4l2_subdev_routing_validate(sd, routing,
  313. V4L2_SUBDEV_ROUTING_ONLY_1_TO_1);
  314. if (ret)
  315. return ret;
  316. return v4l2_subdev_set_routing_with_fmt(sd, state, routing, &format);
  317. }
  318. static int max96714_set_routing(struct v4l2_subdev *sd,
  319. struct v4l2_subdev_state *state,
  320. enum v4l2_subdev_format_whence which,
  321. struct v4l2_subdev_krouting *routing)
  322. {
  323. struct max96714_priv *priv = sd_to_max96714(sd);
  324. if (which == V4L2_SUBDEV_FORMAT_ACTIVE && priv->enabled_source_streams)
  325. return -EBUSY;
  326. return _max96714_set_routing(sd, state, which, routing);
  327. }
  328. static int max96714_init_state(struct v4l2_subdev *sd,
  329. struct v4l2_subdev_state *state)
  330. {
  331. struct v4l2_subdev_route routes[] = {
  332. {
  333. .sink_pad = MAX96714_PAD_SINK,
  334. .sink_stream = 0,
  335. .source_pad = MAX96714_PAD_SOURCE,
  336. .source_stream = 0,
  337. .flags = V4L2_SUBDEV_ROUTE_FL_ACTIVE,
  338. }
  339. };
  340. struct v4l2_subdev_krouting routing = {
  341. .num_routes = ARRAY_SIZE(routes),
  342. .routes = routes,
  343. };
  344. return _max96714_set_routing(sd, state, V4L2_SUBDEV_FORMAT_ACTIVE,
  345. &routing);
  346. }
  347. static const struct v4l2_subdev_pad_ops max96714_pad_ops = {
  348. .enable_streams = max96714_enable_streams,
  349. .disable_streams = max96714_disable_streams,
  350. .set_routing = max96714_set_routing,
  351. .get_fmt = v4l2_subdev_get_fmt,
  352. .set_fmt = max96714_set_fmt,
  353. };
  354. static bool max96714_link_locked(struct max96714_priv *priv)
  355. {
  356. u64 val = 0;
  357. cci_read(priv->regmap, MAX96714_LINK_LOCK, &val, NULL);
  358. return val & MAX96714_LINK_LOCK_BIT;
  359. }
  360. static void max96714_link_status(struct max96714_priv *priv)
  361. {
  362. struct device *dev = &priv->client->dev;
  363. dev_info(dev, "Link locked:%d\n", max96714_link_locked(priv));
  364. }
  365. static bool max96714_pipe_locked(struct max96714_priv *priv)
  366. {
  367. u64 val;
  368. cci_read(priv->regmap, MAX96714_VIDEO_RX8, &val, NULL);
  369. return val & MAX96714_VID_LOCK;
  370. }
  371. static void max96714_pipe_status(struct max96714_priv *priv)
  372. {
  373. struct device *dev = &priv->client->dev;
  374. dev_info(dev, "Pipe vidlock:%d\n", max96714_pipe_locked(priv));
  375. }
  376. static void max96714_csi_status(struct max96714_priv *priv)
  377. {
  378. struct device *dev = &priv->client->dev;
  379. u64 freq = 0;
  380. cci_read(priv->regmap, MAX96714_BACKTOP25, &freq, NULL);
  381. freq = FIELD_GET(CSI_DPLL_FREQ_MASK, freq);
  382. dev_info(dev, "CSI controller DPLL freq:%u00MHz CSIPHY enabled:%d\n",
  383. (u8)freq, max96714_tx_port_enabled(priv));
  384. }
  385. static int max96714_log_status(struct v4l2_subdev *sd)
  386. {
  387. struct max96714_priv *priv = sd_to_max96714(sd);
  388. struct device *dev = &priv->client->dev;
  389. dev_info(dev, "Deserializer: max96714\n");
  390. max96714_link_status(priv);
  391. max96714_pipe_status(priv);
  392. max96714_csi_status(priv);
  393. return 0;
  394. }
  395. static const struct v4l2_subdev_core_ops max96714_subdev_core_ops = {
  396. .log_status = max96714_log_status,
  397. };
  398. static const struct v4l2_subdev_video_ops max96714_video_ops = {
  399. .s_stream = v4l2_subdev_s_stream_helper,
  400. };
  401. static const struct v4l2_subdev_internal_ops max96714_internal_ops = {
  402. .init_state = max96714_init_state,
  403. };
  404. static const struct v4l2_subdev_ops max96714_subdev_ops = {
  405. .video = &max96714_video_ops,
  406. .core = &max96714_subdev_core_ops,
  407. .pad = &max96714_pad_ops,
  408. };
  409. static const struct media_entity_operations max96714_entity_ops = {
  410. .link_validate = v4l2_subdev_link_validate,
  411. };
  412. static int max96714_notify_bound(struct v4l2_async_notifier *notifier,
  413. struct v4l2_subdev *subdev,
  414. struct v4l2_async_connection *asd)
  415. {
  416. struct max96714_priv *priv = sd_to_max96714(notifier->sd);
  417. struct device *dev = &priv->client->dev;
  418. int ret;
  419. ret = media_entity_get_fwnode_pad(&subdev->entity,
  420. priv->rxport.source.ep_fwnode,
  421. MEDIA_PAD_FL_SOURCE);
  422. if (ret < 0) {
  423. dev_err(dev, "Failed to find pad for %s\n", subdev->name);
  424. return ret;
  425. }
  426. priv->rxport.source.sd = subdev;
  427. priv->rxport.source.pad = ret;
  428. ret = media_create_pad_link(&priv->rxport.source.sd->entity,
  429. priv->rxport.source.pad, &priv->sd.entity,
  430. MAX96714_PAD_SINK,
  431. MEDIA_LNK_FL_ENABLED |
  432. MEDIA_LNK_FL_IMMUTABLE);
  433. if (ret) {
  434. dev_err(dev, "Unable to link %s:%u -> %s:%u\n",
  435. priv->rxport.source.sd->name, priv->rxport.source.pad,
  436. priv->sd.name, MAX96714_PAD_SINK);
  437. return ret;
  438. }
  439. return 0;
  440. }
  441. static const struct v4l2_async_notifier_operations max96714_notify_ops = {
  442. .bound = max96714_notify_bound,
  443. };
  444. static int max96714_v4l2_notifier_register(struct max96714_priv *priv)
  445. {
  446. struct device *dev = &priv->client->dev;
  447. struct max96714_rxport *rxport = &priv->rxport;
  448. struct v4l2_async_connection *asd;
  449. int ret;
  450. if (!rxport->source.ep_fwnode)
  451. return 0;
  452. v4l2_async_subdev_nf_init(&priv->notifier, &priv->sd);
  453. asd = v4l2_async_nf_add_fwnode(&priv->notifier,
  454. rxport->source.ep_fwnode,
  455. struct v4l2_async_connection);
  456. if (IS_ERR(asd)) {
  457. dev_err(dev, "Failed to add subdev: %pe", asd);
  458. v4l2_async_nf_cleanup(&priv->notifier);
  459. return PTR_ERR(asd);
  460. }
  461. priv->notifier.ops = &max96714_notify_ops;
  462. ret = v4l2_async_nf_register(&priv->notifier);
  463. if (ret) {
  464. dev_err(dev, "Failed to register subdev_notifier");
  465. v4l2_async_nf_cleanup(&priv->notifier);
  466. return ret;
  467. }
  468. return 0;
  469. }
  470. static int max96714_create_subdev(struct max96714_priv *priv)
  471. {
  472. struct device *dev = &priv->client->dev;
  473. int ret;
  474. v4l2_i2c_subdev_init(&priv->sd, priv->client, &max96714_subdev_ops);
  475. priv->sd.internal_ops = &max96714_internal_ops;
  476. v4l2_ctrl_handler_init(&priv->ctrl_handler, 1);
  477. priv->sd.ctrl_handler = &priv->ctrl_handler;
  478. v4l2_ctrl_new_int_menu(&priv->ctrl_handler, NULL, V4L2_CID_LINK_FREQ,
  479. 0, 0, &priv->tx_link_freq);
  480. v4l2_ctrl_new_std_menu_items(&priv->ctrl_handler,
  481. &max96714_ctrl_ops,
  482. V4L2_CID_TEST_PATTERN,
  483. ARRAY_SIZE(max96714_test_pattern) - 1,
  484. 0, 0, max96714_test_pattern);
  485. if (priv->ctrl_handler.error) {
  486. ret = priv->ctrl_handler.error;
  487. goto err_free_ctrl;
  488. }
  489. priv->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_STREAMS;
  490. priv->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
  491. priv->sd.entity.ops = &max96714_entity_ops;
  492. priv->pads[MAX96714_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  493. priv->pads[MAX96714_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  494. ret = media_entity_pads_init(&priv->sd.entity,
  495. MAX96714_NPORTS,
  496. priv->pads);
  497. if (ret)
  498. goto err_free_ctrl;
  499. priv->sd.state_lock = priv->sd.ctrl_handler->lock;
  500. ret = v4l2_subdev_init_finalize(&priv->sd);
  501. if (ret)
  502. goto err_entity_cleanup;
  503. ret = max96714_v4l2_notifier_register(priv);
  504. if (ret) {
  505. dev_err(dev, "v4l2 subdev notifier register failed: %d\n", ret);
  506. goto err_subdev_cleanup;
  507. }
  508. ret = v4l2_async_register_subdev(&priv->sd);
  509. if (ret) {
  510. dev_err(dev, "v4l2_async_register_subdev error: %d\n", ret);
  511. goto err_unreg_notif;
  512. }
  513. return 0;
  514. err_unreg_notif:
  515. v4l2_async_nf_unregister(&priv->notifier);
  516. v4l2_async_nf_cleanup(&priv->notifier);
  517. err_subdev_cleanup:
  518. v4l2_subdev_cleanup(&priv->sd);
  519. err_entity_cleanup:
  520. media_entity_cleanup(&priv->sd.entity);
  521. err_free_ctrl:
  522. v4l2_ctrl_handler_free(&priv->ctrl_handler);
  523. return ret;
  524. };
  525. static void max96714_destroy_subdev(struct max96714_priv *priv)
  526. {
  527. v4l2_async_nf_unregister(&priv->notifier);
  528. v4l2_async_nf_cleanup(&priv->notifier);
  529. v4l2_async_unregister_subdev(&priv->sd);
  530. v4l2_subdev_cleanup(&priv->sd);
  531. media_entity_cleanup(&priv->sd.entity);
  532. v4l2_ctrl_handler_free(&priv->ctrl_handler);
  533. }
  534. static int max96714_i2c_mux_select(struct i2c_mux_core *mux, u32 chan)
  535. {
  536. return 0;
  537. }
  538. static int max96714_i2c_mux_init(struct max96714_priv *priv)
  539. {
  540. priv->mux = i2c_mux_alloc(priv->client->adapter, &priv->client->dev,
  541. 1, 0, I2C_MUX_LOCKED | I2C_MUX_GATE,
  542. max96714_i2c_mux_select, NULL);
  543. if (!priv->mux)
  544. return -ENOMEM;
  545. return i2c_mux_add_adapter(priv->mux, 0, 0);
  546. }
  547. static int max96714_init_tx_port(struct max96714_priv *priv)
  548. {
  549. struct v4l2_mbus_config_mipi_csi2 *mipi;
  550. unsigned long lanes_used = 0;
  551. unsigned int val, lane;
  552. int ret;
  553. ret = max96714_disable_tx_port(priv);
  554. mipi = &priv->mipi_csi2;
  555. val = div_u64(priv->tx_link_freq * 2, MHZ(100));
  556. cci_update_bits(priv->regmap, MAX96714_BACKTOP25,
  557. CSI_DPLL_FREQ_MASK, val, &ret);
  558. val = FIELD_PREP(MAX96714_CSI2_LANE_CNT_MASK, mipi->num_data_lanes - 1);
  559. cci_update_bits(priv->regmap, MAX96714_MIPI_LANE_CNT,
  560. MAX96714_CSI2_LANE_CNT_MASK, val, &ret);
  561. /* lanes polarity */
  562. val = 0;
  563. for (lane = 0; lane < mipi->num_data_lanes + 1; lane++) {
  564. if (!mipi->lane_polarities[lane])
  565. continue;
  566. if (lane == 0)
  567. /* clock lane */
  568. val |= BIT(5);
  569. else if (lane < 3)
  570. /* Lane D0 and D1 */
  571. val |= BIT(lane - 1);
  572. else
  573. /* D2 and D3 */
  574. val |= BIT(lane);
  575. }
  576. cci_update_bits(priv->regmap, MAX96714_MIPI_POLARITY,
  577. MAX96714_MIPI_POLARITY_MASK, val, &ret);
  578. /* lanes mapping */
  579. val = 0;
  580. for (lane = 0; lane < mipi->num_data_lanes; lane++) {
  581. val |= (mipi->data_lanes[lane] - 1) << (lane * 2);
  582. lanes_used |= BIT(mipi->data_lanes[lane] - 1);
  583. }
  584. /*
  585. * Unused lanes need to be mapped as well to not have
  586. * the same lanes mapped twice.
  587. */
  588. for (; lane < MAX96714_CSI_NLANES; lane++) {
  589. unsigned int idx = find_first_zero_bit(&lanes_used,
  590. MAX96714_CSI_NLANES);
  591. val |= idx << (lane * 2);
  592. lanes_used |= BIT(idx);
  593. }
  594. return cci_write(priv->regmap, MAX96714_MIPI_LANE_MAP, val, &ret);
  595. }
  596. static int max96714_rxport_enable_poc(struct max96714_priv *priv)
  597. {
  598. struct max96714_rxport *rxport = &priv->rxport;
  599. if (!rxport->poc)
  600. return 0;
  601. return regulator_enable(rxport->poc);
  602. }
  603. static int max96714_rxport_disable_poc(struct max96714_priv *priv)
  604. {
  605. struct max96714_rxport *rxport = &priv->rxport;
  606. if (!rxport->poc)
  607. return 0;
  608. return regulator_disable(rxport->poc);
  609. }
  610. static int max96714_parse_dt_txport(struct max96714_priv *priv)
  611. {
  612. struct device *dev = &priv->client->dev;
  613. struct v4l2_fwnode_endpoint vep = { .bus_type = V4L2_MBUS_CSI2_DPHY };
  614. struct fwnode_handle *ep_fwnode;
  615. u32 num_data_lanes;
  616. int ret;
  617. ep_fwnode = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
  618. MAX96714_PAD_SOURCE, 0, 0);
  619. if (!ep_fwnode)
  620. return -EINVAL;
  621. ret = v4l2_fwnode_endpoint_alloc_parse(ep_fwnode, &vep);
  622. fwnode_handle_put(ep_fwnode);
  623. if (ret) {
  624. dev_err(dev, "tx: failed to parse endpoint data\n");
  625. return -EINVAL;
  626. }
  627. if (vep.nr_of_link_frequencies != 1) {
  628. ret = -EINVAL;
  629. goto err_free_vep;
  630. }
  631. priv->tx_link_freq = vep.link_frequencies[0];
  632. /* Min 50MHz, Max 1250MHz, 50MHz step */
  633. if (priv->tx_link_freq < MHZ(50) || priv->tx_link_freq > MHZ(1250) ||
  634. (u32)priv->tx_link_freq % MHZ(50)) {
  635. dev_err(dev, "tx: invalid link frequency\n");
  636. ret = -EINVAL;
  637. goto err_free_vep;
  638. }
  639. num_data_lanes = vep.bus.mipi_csi2.num_data_lanes;
  640. if (num_data_lanes < 1 || num_data_lanes > MAX96714_CSI_NLANES) {
  641. dev_err(dev,
  642. "tx: invalid number of data lanes must be 1 to 4\n");
  643. ret = -EINVAL;
  644. goto err_free_vep;
  645. }
  646. priv->mipi_csi2 = vep.bus.mipi_csi2;
  647. err_free_vep:
  648. v4l2_fwnode_endpoint_free(&vep);
  649. return ret;
  650. }
  651. static int max96714_parse_dt_rxport(struct max96714_priv *priv)
  652. {
  653. static const char *poc_name = "port0-poc";
  654. struct max96714_rxport *rxport = &priv->rxport;
  655. struct device *dev = &priv->client->dev;
  656. struct fwnode_handle *ep_fwnode;
  657. int ret;
  658. ep_fwnode = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
  659. MAX96714_PAD_SINK, 0, 0);
  660. if (!ep_fwnode)
  661. return -ENOENT;
  662. rxport->source.ep_fwnode = fwnode_graph_get_remote_endpoint(ep_fwnode);
  663. fwnode_handle_put(ep_fwnode);
  664. if (!rxport->source.ep_fwnode) {
  665. dev_err(dev, "rx: no remote endpoint\n");
  666. return -EINVAL;
  667. }
  668. rxport->poc = devm_regulator_get_optional(dev, poc_name);
  669. if (IS_ERR(rxport->poc)) {
  670. ret = PTR_ERR(rxport->poc);
  671. if (ret == -ENODEV) {
  672. rxport->poc = NULL;
  673. } else {
  674. dev_err(dev, "rx: failed to get POC supply: %d\n", ret);
  675. goto err_put_source_ep_fwnode;
  676. }
  677. }
  678. return 0;
  679. err_put_source_ep_fwnode:
  680. fwnode_handle_put(rxport->source.ep_fwnode);
  681. return ret;
  682. }
  683. static int max96714_parse_dt(struct max96714_priv *priv)
  684. {
  685. int ret;
  686. ret = max96714_parse_dt_txport(priv);
  687. if (ret)
  688. return ret;
  689. ret = max96714_parse_dt_rxport(priv);
  690. /*
  691. * The deserializer can create a test pattern even if the
  692. * rx port is not connected to a serializer.
  693. */
  694. if (ret && ret == -ENOENT)
  695. ret = 0;
  696. return ret;
  697. }
  698. static int max96714_enable_core_hw(struct max96714_priv *priv)
  699. {
  700. struct device *dev = &priv->client->dev;
  701. u64 val;
  702. int ret;
  703. if (priv->pd_gpio) {
  704. /* wait min 2 ms for reset to complete */
  705. gpiod_set_value_cansleep(priv->pd_gpio, 1);
  706. fsleep(2000);
  707. gpiod_set_value_cansleep(priv->pd_gpio, 0);
  708. /* wait min 2 ms for power up to finish */
  709. fsleep(2000);
  710. }
  711. ret = cci_read(priv->regmap, MAX96714_REG13, &val, NULL);
  712. if (ret) {
  713. dev_err_probe(dev, ret, "Cannot read first register, abort\n");
  714. goto err_pd_gpio;
  715. }
  716. if (val != MAX96714_DEVICE_ID && val != MAX96714F_DEVICE_ID) {
  717. dev_err(dev, "Unsupported device id expected %x got %x\n",
  718. MAX96714F_DEVICE_ID, (u8)val);
  719. ret = -EOPNOTSUPP;
  720. goto err_pd_gpio;
  721. }
  722. ret = cci_read(priv->regmap, MAX96714_DEV_REV, &val, NULL);
  723. if (ret)
  724. goto err_pd_gpio;
  725. dev_dbg(dev, "Found %x (rev %lx)\n", MAX96714F_DEVICE_ID,
  726. (u8)val & MAX96714_DEV_REV_MASK);
  727. ret = cci_read(priv->regmap, MAX96714_MIPI_TX52, &val, NULL);
  728. if (ret)
  729. goto err_pd_gpio;
  730. if (!(val & MAX96714_TUN_EN)) {
  731. dev_err(dev, "Only supporting tunnel mode");
  732. ret = -EOPNOTSUPP;
  733. goto err_pd_gpio;
  734. }
  735. return 0;
  736. err_pd_gpio:
  737. gpiod_set_value_cansleep(priv->pd_gpio, 1);
  738. return ret;
  739. }
  740. static void max96714_disable_core_hw(struct max96714_priv *priv)
  741. {
  742. gpiod_set_value_cansleep(priv->pd_gpio, 1);
  743. }
  744. static int max96714_get_hw_resources(struct max96714_priv *priv)
  745. {
  746. struct device *dev = &priv->client->dev;
  747. priv->regmap = devm_cci_regmap_init_i2c(priv->client, 16);
  748. if (IS_ERR(priv->regmap))
  749. return PTR_ERR(priv->regmap);
  750. priv->pd_gpio =
  751. devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
  752. if (IS_ERR(priv->pd_gpio))
  753. return dev_err_probe(dev, PTR_ERR(priv->pd_gpio),
  754. "Cannot get powerdown GPIO\n");
  755. return 0;
  756. }
  757. static int max96714_probe(struct i2c_client *client)
  758. {
  759. struct device *dev = &client->dev;
  760. struct max96714_priv *priv;
  761. int ret;
  762. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  763. if (!priv)
  764. return -ENOMEM;
  765. priv->client = client;
  766. ret = max96714_get_hw_resources(priv);
  767. if (ret)
  768. return ret;
  769. ret = max96714_enable_core_hw(priv);
  770. if (ret)
  771. return ret;
  772. ret = max96714_parse_dt(priv);
  773. if (ret)
  774. goto err_disable_core_hw;
  775. max96714_init_tx_port(priv);
  776. ret = max96714_rxport_enable_poc(priv);
  777. if (ret)
  778. goto err_free_ports;
  779. ret = max96714_i2c_mux_init(priv);
  780. if (ret)
  781. goto err_disable_poc;
  782. ret = max96714_create_subdev(priv);
  783. if (ret)
  784. goto err_del_mux;
  785. return 0;
  786. err_del_mux:
  787. i2c_mux_del_adapters(priv->mux);
  788. err_disable_poc:
  789. max96714_rxport_disable_poc(priv);
  790. err_free_ports:
  791. fwnode_handle_put(priv->rxport.source.ep_fwnode);
  792. err_disable_core_hw:
  793. max96714_disable_core_hw(priv);
  794. return ret;
  795. }
  796. static void max96714_remove(struct i2c_client *client)
  797. {
  798. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  799. struct max96714_priv *priv = sd_to_max96714(sd);
  800. max96714_destroy_subdev(priv);
  801. i2c_mux_del_adapters(priv->mux);
  802. max96714_rxport_disable_poc(priv);
  803. fwnode_handle_put(priv->rxport.source.ep_fwnode);
  804. max96714_disable_core_hw(priv);
  805. gpiod_set_value_cansleep(priv->pd_gpio, 1);
  806. }
  807. static const struct of_device_id max96714_of_ids[] = {
  808. { .compatible = "maxim,max96714f" },
  809. { }
  810. };
  811. MODULE_DEVICE_TABLE(of, max96714_of_ids);
  812. static struct i2c_driver max96714_i2c_driver = {
  813. .driver = {
  814. .name = "max96714",
  815. .of_match_table = max96714_of_ids,
  816. },
  817. .probe = max96714_probe,
  818. .remove = max96714_remove,
  819. };
  820. module_i2c_driver(max96714_i2c_driver);
  821. MODULE_LICENSE("GPL");
  822. MODULE_DESCRIPTION("Maxim Integrated GMSL2 Deserializers Driver");
  823. MODULE_AUTHOR("Julien Massot <julien.massot@collabora.com>");