Re: [PATCH RFC 2/2] phy: phy-can-transceiver: Add support for setting mux

From: Peter Rosin
Date: Fri Nov 12 2021 - 14:15:57 EST


Hi!

On 2021-11-12 14:48, Aswath Govindraju wrote:
> Hi Marc,
>
> On 12/11/21 2:10 pm, Marc Kleine-Budde wrote:
>> On 11.11.2021 22:13:12, Aswath Govindraju wrote:
>>> On some boards, for routing CAN signals from controller to transceiver,
>>> muxes might need to be set. Therefore, add support for setting the mux by
>>> reading the mux-controls property from the device tree node.
>>>
>>> Signed-off-by: Aswath Govindraju <a-govindraju@xxxxxx>
>>> ---
>>> drivers/phy/phy-can-transceiver.c | 21 +++++++++++++++++++++
>>> 1 file changed, 21 insertions(+)
>>>
>>> diff --git a/drivers/phy/phy-can-transceiver.c b/drivers/phy/phy-can-transceiver.c
>>> index 6f3fe37dee0e..3d8da5226e27 100644
>>> --- a/drivers/phy/phy-can-transceiver.c
>>> +++ b/drivers/phy/phy-can-transceiver.c
>>> @@ -10,6 +10,7 @@
>>> #include<linux/module.h>
>>> #include<linux/gpio.h>
>>> #include<linux/gpio/consumer.h>
>>> +#include <linux/mux/consumer.h>
>>>
>>> struct can_transceiver_data {
>>> u32 flags;
>>> @@ -21,13 +22,22 @@ struct can_transceiver_phy {
>>> struct phy *generic_phy;
>>> struct gpio_desc *standby_gpio;
>>> struct gpio_desc *enable_gpio;
>>> + struct mux_control *mux_ctrl;
>>> };
>>>
>>> /* Power on function */
>>> static int can_transceiver_phy_power_on(struct phy *phy)
>>> {
>>> + int ret;
>>> struct can_transceiver_phy *can_transceiver_phy = phy_get_drvdata(phy);
>>>
>>> + if (can_transceiver_phy->mux_ctrl) {
>>> + ret = mux_control_select(can_transceiver_phy->mux_ctrl, 1);
>>
>> Hard coding the "1" looks wrong here. I have seen some boards where you
>> can select between a CAN-2.0 and a single wire CAN transceiver with a
>> mux. So I think we cannot hard code the "1" here.
>>
>
> Yes, as you mentioned it is not ideal to hard code "1". I feel that, it
> would be much better to read the state of the mux to be set from the
> mux-controls property. The issue that I see with this approach is that
> the current implementation in the mux framework only allows for one
> argument, which is for indicating the line to be toggled in the mux. If
> more arguments are added then an error is returned from the
> "mux_control_get". I am not sure why this limitation was added.

The only current use of the first argument is for mux chips that contain
more than one mux control. The limit in the mux core is there since no
mux driver need more than this one argument. The number of mux-control
property arguments is fixed by the #mux-control-cells property in the
mux-control node. I don't see any way to and a new optional mux-control
property argument that specifies a specific state. How would that not
break all existing users?

The current mux interface is designed around the idea that you wrap a
mux control in a mux (lacking better name) application. There are
several such mux applications in the tree, those for I2C, IIO and SPI
pops into my head, and that you then tie the end user consumer to this
muxing application. The mux state comes as a part of how you have tied
the end user consumer to the mux application and is not really something
that the mux-control is involved in.

In other words, a mux-control is not really designed to be used directly
by a driver that needs only one of the states.

However, I'm not saying that doing so isn't also a useful model. It
cetainly sound like it could be. However, the reason it's not done that
way is that I did not want to add muxing code to *all* drivers. I.e. it
would not be flexible to have to add boilerplate mux code to each and
every IIO driver that happen to be connected in a way that a mux has to
be in a certain state for the signal to reach the ADC (or whatever).
Instead, new IIO channels are created for the appropriate mux states
and the IIO mux is connected to the parent IIO channel. When one of the
muxed channels is accessed the mux is selected as needed, and the ADC
driver needs to know nothing about it. If two muxes need to be in a
certain position, you again have no need to "pollute" drivers with
double builerplate mux code. Instead, you simply add two levels of
muxing to the muxed IIO channel.

I think the same is probably true in this case too, and that it would
perhaps be better to create a mux application for phys? But I don't know
what the phy structure looks like, so I'm not in a position to say for
sure if this model fits. But I imagine that phys have providers and
consumers and that a mux can be jammed in there in some way and
intercept some api such that the needed mux state can be selected when
needed.

Cheers,
Peter

>
>>> + if (ret) {
>>> + dev_err(&phy->dev, "Failed to select CAN mux: %d\n", ret);
>>> + return ret;
>>> + }
>>> + }
>>> if (can_transceiver_phy->standby_gpio)
>>> gpiod_set_value_cansleep(can_transceiver_phy->standby_gpio, 0);
>>> if (can_transceiver_phy->enable_gpio)
>>> @@ -45,6 +55,8 @@ static int can_transceiver_phy_power_off(struct phy *phy)
>>> gpiod_set_value_cansleep(can_transceiver_phy->standby_gpio, 1);
>>> if (can_transceiver_phy->enable_gpio)
>>> gpiod_set_value_cansleep(can_transceiver_phy->enable_gpio, 0);
>>> + if (can_transceiver_phy->mux_ctrl)
>>> + mux_control_deselect(can_transceiver_phy->mux_ctrl);
>>>
>>> return 0;
>>> }
>>> @@ -95,6 +107,15 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
>>> match = of_match_node(can_transceiver_phy_ids, pdev->dev.of_node);
>>> drvdata = match->data;
>>>
>>> + if (of_property_read_bool(dev->of_node, "mux-controls")) {
>>
>> Is this the proper way of doing this? Looks like we need a
>> devm_mux_control_get_optional(), which doesn't return a -ENODEV if the
>> device doesn't exist.
>>
>> Cc'ed Peter Rosin.
>>
>>> + struct mux_control *control;
>>> +
>>> + control = devm_mux_control_get(dev, NULL);
>>> + if (IS_ERR(control))
>>> + return PTR_ERR(control);
>>
>> What about making use of dev_err_probe()?
>>
>
> Sure, I will make this change.
>
> Thank you for the comments.
>
> Regards,
> Aswath
>
>>> + can_transceiver_phy->mux_ctrl = control;
>>> + }
>>> +
>>> phy = devm_phy_create(dev, dev->of_node,
>>> &can_transceiver_phy_ops);
>>> if (IS_ERR(phy)) {
>>> --
>>> 2.17.1
>>>
>>>
>>
>> Regards,
>> Marc
>>
>