Lines Matching +full:gpio +full:- +full:poc

1 // SPDX-License-Identifier: GPL-2.0+
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
16 #include <linux/gpio/consumer.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/gpio/machine.h>
20 #include <linux/i2c-mux.h>
27 #include <media/v4l2-async.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-fwnode.h>
31 #include <media/v4l2-subdev.h>
68 #define MAX9286_CSILANECNT(n) (((n) - 1) << 6)
123 #define MAX9286_REV_AMP(n) ((((n) - 30) / 10) << 1) /* in mV */
128 #define MAX9286_REV_FLEN(n) ((n) - 20)
181 struct gpio_chip gpio; member
220 source = &priv->sources[0]; in next_source()
224 for (; source < &priv->sources[MAX9286_NUM_GMSL]; source++) { in next_source()
225 if (source->fwnode) in next_source()
235 #define to_index(priv, source) ((source) - &(priv)->sources[0])
281 /* -----------------------------------------------------------------------------
289 ret = i2c_smbus_read_byte_data(priv->client, reg); in max9286_read()
291 dev_err(&priv->client->dev, in max9286_read()
302 ret = i2c_smbus_write_byte_data(priv->client, reg, val); in max9286_write()
304 dev_err(&priv->client->dev, in max9286_write()
311 /* -----------------------------------------------------------------------------
331 priv->mux_open = true; in max9286_i2c_mux_open()
343 priv->mux_open = false; in max9286_i2c_mux_close()
344 priv->mux_channel = -1; in max9286_i2c_mux_close()
352 if (priv->mux_open) in max9286_i2c_mux_select()
355 if (priv->mux_channel == chan) in max9286_i2c_mux_select()
358 priv->mux_channel = chan; in max9286_i2c_mux_select()
371 if (!i2c_check_functionality(priv->client->adapter, in max9286_i2c_mux_init()
373 return -ENODEV; in max9286_i2c_mux_init()
375 priv->mux = i2c_mux_alloc(priv->client->adapter, &priv->client->dev, in max9286_i2c_mux_init()
376 priv->nsources, 0, I2C_MUX_LOCKED, in max9286_i2c_mux_init()
378 if (!priv->mux) in max9286_i2c_mux_init()
379 return -ENOMEM; in max9286_i2c_mux_init()
381 priv->mux->priv = priv; in max9286_i2c_mux_init()
386 ret = i2c_mux_add_adapter(priv->mux, 0, index, 0); in max9286_i2c_mux_init()
394 i2c_mux_del_adapters(priv->mux); in max9286_i2c_mux_init()
401 priv->i2c_mstbt; in max9286_configure_i2c()
415 if (priv->rev_chan_mv == chan_amplitude) in max9286_reverse_channel_setup()
418 priv->rev_chan_mv = chan_amplitude; in max9286_reverse_channel_setup()
426 * - Enable custom reverse channel configuration (through register 0x3f) in max9286_reverse_channel_setup()
428 * - Adjust reverse channel amplitude: values > 130 are programmed in max9286_reverse_channel_setup()
435 chan_amplitude = max(30U, chan_amplitude - 100); in max9286_reverse_channel_setup()
443 * max9286_check_video_links() - Make sure video links are detected and locked
448 * Returns 0 for success, -EIO for errors.
457 * The delay is not characterized in de-serializer manual, wait up in max9286_check_video_links()
463 return -EIO; in max9286_check_video_links()
465 if ((ret & MAX9286_VIDEO_DETECT_MASK) == priv->source_mask) in max9286_check_video_links()
472 dev_err(&priv->client->dev, in max9286_check_video_links()
474 return -EIO; in max9286_check_video_links()
481 return -EIO; in max9286_check_video_links()
490 dev_err(&priv->client->dev, "Not all enabled links locked\n"); in max9286_check_video_links()
491 return -EIO; in max9286_check_video_links()
498 * max9286_check_config_link() - Detect and wait for configuration links
502 * Returns 0 for success, -EIO for errors.
519 return -EIO; in max9286_check_config_link()
529 dev_err(&priv->client->dev, in max9286_check_config_link()
532 return -EIO; in max9286_check_config_link()
535 dev_info(&priv->client->dev, in max9286_check_config_link()
549 if (max9286_formats[i].code == format->code) { in max9286_set_video_format()
561 * in external GPI-to-GPO mode. in max9286_set_video_format()
566 /* Enable CSI-2 Lane D0-D3 only, DBL mode. */ in max9286_set_video_format()
568 MAX9286_CSILANECNT(priv->csi2_data_lanes) | in max9286_set_video_format()
569 info->datatype); in max9286_set_video_format()
583 if (!priv->interval.numerator || !priv->interval.denominator) { in max9286_set_fsync_period()
599 fsync = div_u64((u64)priv->pixelrate * priv->interval.numerator, in max9286_set_fsync_period()
600 priv->interval.denominator); in max9286_set_fsync_period()
602 dev_dbg(&priv->client->dev, "fsync period %u (pclk %u)\n", fsync, in max9286_set_fsync_period()
603 priv->pixelrate); in max9286_set_fsync_period()
613 /* -----------------------------------------------------------------------------
627 ctrl = v4l2_ctrl_find(source->sd->ctrl_handler, in max9286_set_pixelrate()
639 dev_err(&priv->client->dev, in max9286_set_pixelrate()
641 return -EINVAL; in max9286_set_pixelrate()
646 dev_err(&priv->client->dev, in max9286_set_pixelrate()
648 return -EINVAL; in max9286_set_pixelrate()
651 priv->pixelrate = pixelrate; in max9286_set_pixelrate()
654 * The CSI-2 transmitter pixel rate is the single source rate multiplied in max9286_set_pixelrate()
657 return v4l2_ctrl_s_ctrl_int64(priv->pixelrate_ctrl, in max9286_set_pixelrate()
658 pixelrate * priv->nsources); in max9286_set_pixelrate()
665 struct max9286_priv *priv = sd_to_max9286(notifier->sd); in max9286_notify_bound()
666 struct max9286_source *source = to_max9286_asd(asd)->source; in max9286_notify_bound()
671 ret = media_entity_get_fwnode_pad(&subdev->entity, in max9286_notify_bound()
672 source->fwnode, in max9286_notify_bound()
675 dev_err(&priv->client->dev, in max9286_notify_bound()
676 "Failed to find pad for %s\n", subdev->name); in max9286_notify_bound()
680 priv->bound_sources |= BIT(index); in max9286_notify_bound()
681 source->sd = subdev; in max9286_notify_bound()
684 ret = media_create_pad_link(&source->sd->entity, src_pad, in max9286_notify_bound()
685 &priv->sd.entity, index, in max9286_notify_bound()
689 dev_err(&priv->client->dev, in max9286_notify_bound()
690 "Unable to link %s:%u -> %s:%u\n", in max9286_notify_bound()
691 source->sd->name, src_pad, priv->sd.name, index); in max9286_notify_bound()
695 dev_dbg(&priv->client->dev, "Bound %s pad: %u on index %u\n", in max9286_notify_bound()
696 subdev->name, src_pad, index); in max9286_notify_bound()
703 if (priv->bound_sources != priv->source_mask) in max9286_notify_bound()
710 * - Increase the reverse channel amplitude to compensate for the in max9286_notify_bound()
712 * - Verify all configuration links are properly detected in max9286_notify_bound()
713 * - Disable auto-ack as communication on the control channel are now in max9286_notify_bound()
717 max9286_check_config_link(priv, priv->source_mask); in max9286_notify_bound()
727 struct max9286_priv *priv = sd_to_max9286(notifier->sd); in max9286_notify_unbind()
728 struct max9286_source *source = to_max9286_asd(asd)->source; in max9286_notify_unbind()
731 source->sd = NULL; in max9286_notify_unbind()
732 priv->bound_sources &= ~BIT(index); in max9286_notify_unbind()
742 struct device *dev = &priv->client->dev; in max9286_v4l2_notifier_register()
746 if (!priv->nsources) in max9286_v4l2_notifier_register()
749 v4l2_async_subdev_nf_init(&priv->notifier, &priv->sd); in max9286_v4l2_notifier_register()
755 mas = v4l2_async_nf_add_fwnode(&priv->notifier, source->fwnode, in max9286_v4l2_notifier_register()
760 v4l2_async_nf_cleanup(&priv->notifier); in max9286_v4l2_notifier_register()
764 mas->source = source; in max9286_v4l2_notifier_register()
767 priv->notifier.ops = &max9286_notify_ops; in max9286_v4l2_notifier_register()
769 ret = v4l2_async_nf_register(&priv->notifier); in max9286_v4l2_notifier_register()
772 v4l2_async_nf_cleanup(&priv->notifier); in max9286_v4l2_notifier_register()
781 if (!priv->nsources) in max9286_v4l2_notifier_unregister()
784 v4l2_async_nf_unregister(&priv->notifier); in max9286_v4l2_notifier_unregister()
785 v4l2_async_nf_cleanup(&priv->notifier); in max9286_v4l2_notifier_unregister()
803 format = &priv->fmt[__ffs(priv->bound_sources)]; in max9286_s_stream()
810 * reverse channel as GPIO. We must open all channels while in max9286_s_stream()
817 ret = v4l2_subdev_call(source->sd, video, s_stream, 1); in max9286_s_stream()
843 dev_err(&priv->client->dev, in max9286_s_stream()
845 return -EXDEV; /* Invalid cross-device link */ in max9286_s_stream()
849 * Configure the CSI-2 output to line interleaved mode (W x (N in max9286_s_stream()
851 * images stitched side-by-side) and enable it. in max9286_s_stream()
863 v4l2_subdev_call(source->sd, video, s_stream, 0); in max9286_s_stream()
876 if (interval->pad != MAX9286_SRC_PAD) in max9286_g_frame_interval()
877 return -EINVAL; in max9286_g_frame_interval()
879 interval->interval = priv->interval; in max9286_g_frame_interval()
889 if (interval->pad != MAX9286_SRC_PAD) in max9286_s_frame_interval()
890 return -EINVAL; in max9286_s_frame_interval()
892 priv->interval = interval->interval; in max9286_s_frame_interval()
901 if (code->pad || code->index > 0) in max9286_enum_mbus_code()
902 return -EINVAL; in max9286_enum_mbus_code()
904 code->code = MEDIA_BUS_FMT_UYVY8_1X16; in max9286_enum_mbus_code()
916 return v4l2_subdev_get_try_format(&priv->sd, sd_state, pad); in max9286_get_pad_format()
918 return &priv->fmt[pad]; in max9286_get_pad_format()
932 if (format->pad == MAX9286_SRC_PAD) in max9286_set_fmt()
933 return -EINVAL; in max9286_set_fmt()
937 if (max9286_formats[i].code == format->format.code) in max9286_set_fmt()
942 format->format.code = max9286_formats[0].code; in max9286_set_fmt()
944 cfg_fmt = max9286_get_pad_format(priv, sd_state, format->pad, in max9286_set_fmt()
945 format->which); in max9286_set_fmt()
947 return -EINVAL; in max9286_set_fmt()
949 mutex_lock(&priv->mutex); in max9286_set_fmt()
950 *cfg_fmt = format->format; in max9286_set_fmt()
951 mutex_unlock(&priv->mutex); in max9286_set_fmt()
962 unsigned int pad = format->pad; in max9286_get_fmt()
971 pad = __ffs(priv->bound_sources); in max9286_get_fmt()
973 cfg_fmt = max9286_get_pad_format(priv, sd_state, pad, format->which); in max9286_get_fmt()
975 return -EINVAL; in max9286_get_fmt()
977 mutex_lock(&priv->mutex); in max9286_get_fmt()
978 format->format = *cfg_fmt; in max9286_get_fmt()
979 mutex_unlock(&priv->mutex); in max9286_get_fmt()
1023 format = v4l2_subdev_get_try_format(subdev, fh->state, i); in max9286_open()
1040 switch (ctrl->id) { in max9286_s_ctrl()
1044 return -EINVAL; in max9286_s_ctrl()
1054 struct device *dev = &priv->client->dev; in max9286_v4l2_register()
1068 max9286_init_format(&priv->fmt[i]); in max9286_v4l2_register()
1070 v4l2_i2c_subdev_init(&priv->sd, priv->client, &max9286_subdev_ops); in max9286_v4l2_register()
1071 priv->sd.internal_ops = &max9286_subdev_internal_ops; in max9286_v4l2_register()
1072 priv->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in max9286_v4l2_register()
1074 v4l2_ctrl_handler_init(&priv->ctrls, 1); in max9286_v4l2_register()
1075 priv->pixelrate_ctrl = v4l2_ctrl_new_std(&priv->ctrls, in max9286_v4l2_register()
1080 priv->sd.ctrl_handler = &priv->ctrls; in max9286_v4l2_register()
1081 ret = priv->ctrls.error; in max9286_v4l2_register()
1085 priv->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE; in max9286_v4l2_register()
1086 priv->sd.entity.ops = &max9286_media_ops; in max9286_v4l2_register()
1088 priv->pads[MAX9286_SRC_PAD].flags = MEDIA_PAD_FL_SOURCE; in max9286_v4l2_register()
1090 priv->pads[i].flags = MEDIA_PAD_FL_SINK; in max9286_v4l2_register()
1091 ret = media_entity_pads_init(&priv->sd.entity, MAX9286_N_PADS, in max9286_v4l2_register()
1092 priv->pads); in max9286_v4l2_register()
1096 ret = v4l2_async_register_subdev(&priv->sd); in max9286_v4l2_register()
1105 v4l2_ctrl_handler_free(&priv->ctrls); in max9286_v4l2_register()
1113 v4l2_ctrl_handler_free(&priv->ctrls); in max9286_v4l2_unregister()
1114 v4l2_async_unregister_subdev(&priv->sd); in max9286_v4l2_unregister()
1118 /* -----------------------------------------------------------------------------
1158 max9286_reverse_channel_setup(priv, priv->init_rev_chan_mv); in max9286_setup()
1164 max9286_write(priv, 0x00, MAX9286_MSTLINKSEL_AUTO | priv->route_mask); in max9286_setup()
1165 max9286_write(priv, 0x0b, link_order[priv->route_mask]); in max9286_setup()
1166 max9286_write(priv, 0x69, (0xf & ~priv->route_mask)); in max9286_setup()
1175 dev_dbg(&priv->client->dev, "power-up config: %s immunity, %u-bit bus\n", in max9286_setup()
1179 if (priv->bus_width) { in max9286_setup()
1182 if (priv->bus_width == 27) in max9286_setup()
1184 else if (priv->bus_width == 32) in max9286_setup()
1215 priv->gpio_state |= BIT(offset); in max9286_gpio_set()
1217 priv->gpio_state &= ~BIT(offset); in max9286_gpio_set()
1220 MAX9286_0X0F_RESERVED | priv->gpio_state); in max9286_gpio_set()
1235 return priv->gpio_state & BIT(offset); in max9286_gpiochip_get()
1240 struct device *dev = &priv->client->dev; in max9286_register_gpio()
1241 struct gpio_chip *gpio = &priv->gpio; in max9286_register_gpio() local
1244 /* Configure the GPIO */ in max9286_register_gpio()
1245 gpio->label = dev_name(dev); in max9286_register_gpio()
1246 gpio->parent = dev; in max9286_register_gpio()
1247 gpio->owner = THIS_MODULE; in max9286_register_gpio()
1248 gpio->ngpio = 2; in max9286_register_gpio()
1249 gpio->base = -1; in max9286_register_gpio()
1250 gpio->set = max9286_gpiochip_set; in max9286_register_gpio()
1251 gpio->get = max9286_gpiochip_get; in max9286_register_gpio()
1252 gpio->can_sleep = true; in max9286_register_gpio()
1254 ret = devm_gpiochip_add_data(dev, gpio, priv); in max9286_register_gpio()
1263 struct device *dev = &priv->client->dev; in max9286_parse_gpios()
1267 * Parse the "gpio-poc" vendor property. If the property is not in max9286_parse_gpios()
1270 ret = of_property_read_u32_array(dev->of_node, "maxim,gpio-poc", in max9286_parse_gpios()
1271 priv->gpio_poc, 2); in max9286_parse_gpios()
1272 if (ret == -EINVAL) { in max9286_parse_gpios()
1274 * If gpio lines are not used for the camera power, register in max9286_parse_gpios()
1275 * a gpio controller for consumers. in max9286_parse_gpios()
1281 if (ret || priv->gpio_poc[0] > 1 || in max9286_parse_gpios()
1282 (priv->gpio_poc[1] != GPIO_ACTIVE_HIGH && in max9286_parse_gpios()
1283 priv->gpio_poc[1] != GPIO_ACTIVE_LOW)) { in max9286_parse_gpios()
1284 dev_err(dev, "Invalid 'gpio-poc' property\n"); in max9286_parse_gpios()
1285 return -EINVAL; in max9286_parse_gpios()
1288 priv->use_gpio_poc = true; in max9286_parse_gpios()
1299 if (priv->regulator) in max9286_poc_power_on()
1300 return regulator_enable(priv->regulator); in max9286_poc_power_on()
1302 if (priv->use_gpio_poc) in max9286_poc_power_on()
1303 return max9286_gpio_set(priv, priv->gpio_poc[0], in max9286_poc_power_on()
1304 !priv->gpio_poc[1]); in max9286_poc_power_on()
1306 /* Otherwise use the per-port regulators. */ in max9286_poc_power_on()
1308 ret = regulator_enable(source->regulator); in max9286_poc_power_on()
1320 regulator_disable(source->regulator); in max9286_poc_power_on()
1331 if (priv->regulator) in max9286_poc_power_off()
1332 return regulator_disable(priv->regulator); in max9286_poc_power_off()
1334 if (priv->use_gpio_poc) in max9286_poc_power_off()
1335 return max9286_gpio_set(priv, priv->gpio_poc[0], in max9286_poc_power_off()
1336 priv->gpio_poc[1]); in max9286_poc_power_off()
1341 err = regulator_disable(source->regulator); in max9286_poc_power_off()
1359 dev_err(&priv->client->dev, "Unable to turn power %s\n", in max9286_poc_enable()
1367 struct i2c_client *client = priv->client; in max9286_init()
1376 dev_err(&client->dev, "Unable to setup max9286\n"); in max9286_init()
1386 dev_err(&client->dev, "Failed to register with V4L2\n"); in max9286_init()
1392 dev_err(&client->dev, "Unable to initialize I2C multiplexer\n"); in max9286_init()
1414 fwnode_handle_put(source->fwnode); in max9286_cleanup_dt()
1415 source->fwnode = NULL; in max9286_cleanup_dt()
1421 struct device *dev = &priv->client->dev; in max9286_parse_dt()
1430 of_node_get(dev->of_node); in max9286_parse_dt()
1431 i2c_mux = of_find_node_by_name(dev->of_node, "i2c-mux"); in max9286_parse_dt()
1433 dev_err(dev, "Failed to find i2c-mux node\n"); in max9286_parse_dt()
1434 return -EINVAL; in max9286_parse_dt()
1437 /* Identify which i2c-mux channels are enabled */ in max9286_parse_dt()
1455 for_each_endpoint_of_node(dev->of_node, node) { in max9286_parse_dt()
1483 priv->csi2_data_lanes = in max9286_parse_dt()
1493 if (priv->sources[ep.port].fwnode) { in max9286_parse_dt()
1501 source = &priv->sources[ep.port]; in max9286_parse_dt()
1502 source->fwnode = fwnode_graph_get_remote_endpoint( in max9286_parse_dt()
1504 if (!source->fwnode) { in max9286_parse_dt()
1512 priv->source_mask |= BIT(ep.port); in max9286_parse_dt()
1513 priv->nsources++; in max9286_parse_dt()
1516 of_property_read_u32(dev->of_node, "maxim,bus-width", &priv->bus_width); in max9286_parse_dt()
1517 switch (priv->bus_width) { in max9286_parse_dt()
1528 dev_err(dev, "Invalid %s value %u\n", "maxim,bus-width", in max9286_parse_dt()
1529 priv->bus_width); in max9286_parse_dt()
1530 return -EINVAL; in max9286_parse_dt()
1533 of_property_read_u32(dev->of_node, "maxim,i2c-remote-bus-hz", in max9286_parse_dt()
1538 if (speed->rate == i2c_clk_freq) { in max9286_parse_dt()
1539 priv->i2c_mstbt = speed->mstbt; in max9286_parse_dt()
1545 dev_err(dev, "Invalid %s value %u\n", "maxim,i2c-remote-bus-hz", in max9286_parse_dt()
1547 return -EINVAL; in max9286_parse_dt()
1557 if (of_property_read_u32(dev->of_node, in max9286_parse_dt()
1558 "maxim,reverse-channel-microvolt", in max9286_parse_dt()
1560 priv->init_rev_chan_mv = 170; in max9286_parse_dt()
1562 priv->init_rev_chan_mv = reverse_channel_microvolt / 1000U; in max9286_parse_dt()
1564 priv->route_mask = priv->source_mask; in max9286_parse_dt()
1571 struct device *dev = &priv->client->dev; in max9286_get_poc_supplies()
1576 priv->regulator = devm_regulator_get_optional(dev, "poc"); in max9286_get_poc_supplies()
1577 if (!IS_ERR(priv->regulator)) in max9286_get_poc_supplies()
1580 if (PTR_ERR(priv->regulator) != -ENODEV) in max9286_get_poc_supplies()
1581 return dev_err_probe(dev, PTR_ERR(priv->regulator), in max9286_get_poc_supplies()
1582 "Unable to get PoC regulator\n"); in max9286_get_poc_supplies()
1584 /* If there's no global regulator, get per-port regulators. */ in max9286_get_poc_supplies()
1586 "No global PoC regulator, looking for per-port regulators\n"); in max9286_get_poc_supplies()
1587 priv->regulator = NULL; in max9286_get_poc_supplies()
1593 snprintf(name, sizeof(name), "port%u-poc", index); in max9286_get_poc_supplies()
1594 source->regulator = devm_regulator_get(dev, name); in max9286_get_poc_supplies()
1595 if (IS_ERR(source->regulator)) { in max9286_get_poc_supplies()
1596 ret = PTR_ERR(source->regulator); in max9286_get_poc_supplies()
1598 "Unable to get port %u PoC regulator\n", in max9286_get_poc_supplies()
1612 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL); in max9286_probe()
1614 return -ENOMEM; in max9286_probe()
1616 mutex_init(&priv->mutex); in max9286_probe()
1618 priv->client = client; in max9286_probe()
1620 /* GPIO values default to high */ in max9286_probe()
1621 priv->gpio_state = BIT(0) | BIT(1); in max9286_probe()
1627 priv->gpiod_pwdn = devm_gpiod_get_optional(&client->dev, "enable", in max9286_probe()
1629 if (IS_ERR(priv->gpiod_pwdn)) { in max9286_probe()
1630 ret = PTR_ERR(priv->gpiod_pwdn); in max9286_probe()
1634 gpiod_set_consumer_name(priv->gpiod_pwdn, "max9286-pwdn"); in max9286_probe()
1635 gpiod_set_value_cansleep(priv->gpiod_pwdn, 1); in max9286_probe()
1638 if (priv->gpiod_pwdn) in max9286_probe()
1649 * The MAX9286 initialises with auto-acknowledge enabled by default. in max9286_probe()
1659 if (!priv->use_gpio_poc) { in max9286_probe()
1672 gpiod_set_value_cansleep(priv->gpiod_pwdn, 0); in max9286_probe()
1683 i2c_mux_del_adapters(priv->mux); in max9286_remove()
1689 gpiod_set_value_cansleep(priv->gpiod_pwdn, 0); in max9286_remove()