Skip to content

Commit 1ddff7d

Browse files
shcgitmarckleinebudde
authored andcommitted
can: mcp251x: Replace power callbacks with regulator API
This patch replaces power callbacks to the regulator API. To improve the readability of the code, helper for the regulator enable/disable was added. Acked-by: Haojian Zhuang <haojian.zhuang@gmail.com> Signed-off-by: Alexander Shiyan <shc_work@mail.ru> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
1 parent 954c396 commit 1ddff7d

4 files changed

Lines changed: 69 additions & 76 deletions

File tree

‎arch/arm/mach-pxa/icontrol.c‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ static struct pxa2xx_spi_chip mcp251x_chip_info4 = {
7373

7474
static struct mcp251x_platform_data mcp251x_info = {
7575
.oscillator_frequency = 16E6,
76-
.board_specific_setup = NULL,
77-
.power_enable = NULL,
78-
.transceiver_enable = NULL
7976
};
8077

8178
static struct spi_board_info mcp251x_board_info[] = {

‎arch/arm/mach-pxa/zeus.c‎

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include <linux/i2c/pca953x.h>
3030
#include <linux/apm-emulation.h>
3131
#include <linux/can/platform/mcp251x.h>
32+
#include <linux/regulator/fixed.h>
33+
#include <linux/regulator/machine.h>
3234

3335
#include <asm/mach-types.h>
3436
#include <asm/suspend.h>
@@ -391,33 +393,34 @@ static struct pxa2xx_spi_master pxa2xx_spi_ssp3_master_info = {
391393
};
392394

393395
/* CAN bus on SPI */
394-
static int zeus_mcp2515_setup(struct spi_device *sdev)
395-
{
396-
int err;
397-
398-
err = gpio_request(ZEUS_CAN_SHDN_GPIO, "CAN shutdown");
399-
if (err)
400-
return err;
396+
static struct regulator_consumer_supply can_regulator_consumer =
397+
REGULATOR_SUPPLY("vdd", "spi3.0");
401398

402-
err = gpio_direction_output(ZEUS_CAN_SHDN_GPIO, 1);
403-
if (err) {
404-
gpio_free(ZEUS_CAN_SHDN_GPIO);
405-
return err;
406-
}
399+
static struct regulator_init_data can_regulator_init_data = {
400+
.constraints = {
401+
.valid_ops_mask = REGULATOR_CHANGE_STATUS,
402+
},
403+
.consumer_supplies = &can_regulator_consumer,
404+
.num_consumer_supplies = 1,
405+
};
407406

408-
return 0;
409-
}
407+
static struct fixed_voltage_config can_regulator_pdata = {
408+
.supply_name = "CAN_SHDN",
409+
.microvolts = 3300000,
410+
.gpio = ZEUS_CAN_SHDN_GPIO,
411+
.init_data = &can_regulator_init_data,
412+
};
410413

411-
static int zeus_mcp2515_transceiver_enable(int enable)
412-
{
413-
gpio_set_value(ZEUS_CAN_SHDN_GPIO, !enable);
414-
return 0;
415-
}
414+
static struct platform_device can_regulator_device = {
415+
.name = "reg-fixed-volage",
416+
.id = -1,
417+
.dev = {
418+
.platform_data = &can_regulator_pdata,
419+
},
420+
};
416421

417422
static struct mcp251x_platform_data zeus_mcp2515_pdata = {
418423
.oscillator_frequency = 16*1000*1000,
419-
.board_specific_setup = zeus_mcp2515_setup,
420-
.power_enable = zeus_mcp2515_transceiver_enable,
421424
};
422425

423426
static struct spi_board_info zeus_spi_board_info[] = {
@@ -516,6 +519,7 @@ static struct platform_device *zeus_devices[] __initdata = {
516519
&zeus_leds_device,
517520
&zeus_pcmcia_device,
518521
&zeus_max6369_device,
522+
&can_regulator_device,
519523
};
520524

521525
/* AC'97 */

‎drivers/net/can/mcp251x.c‎

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737
*
3838
* static struct mcp251x_platform_data mcp251x_info = {
3939
* .oscillator_frequency = 8000000,
40-
* .board_specific_setup = &mcp251x_setup,
41-
* .power_enable = mcp251x_power_enable,
42-
* .transceiver_enable = NULL,
4340
* };
4441
*
4542
* static struct spi_board_info spi_board_info[] = {
@@ -76,6 +73,7 @@
7673
#include <linux/slab.h>
7774
#include <linux/spi/spi.h>
7875
#include <linux/uaccess.h>
76+
#include <linux/regulator/consumer.h>
7977

8078
/* SPI interface instruction set */
8179
#define INSTRUCTION_WRITE 0x02
@@ -264,6 +262,8 @@ struct mcp251x_priv {
264262
#define AFTER_SUSPEND_POWER 4
265263
#define AFTER_SUSPEND_RESTART 8
266264
int restart_tx;
265+
struct regulator *power;
266+
struct regulator *transceiver;
267267
};
268268

269269
#define MCP251X_IS(_model) \
@@ -667,24 +667,32 @@ static int mcp251x_hw_probe(struct spi_device *spi)
667667
return (st1 == 0x80 && st2 == 0x07) ? 1 : 0;
668668
}
669669

670+
static int mcp251x_power_enable(struct regulator *reg, int enable)
671+
{
672+
if (IS_ERR(reg))
673+
return 0;
674+
675+
if (enable)
676+
return regulator_enable(reg);
677+
else
678+
return regulator_disable(reg);
679+
}
680+
670681
static void mcp251x_open_clean(struct net_device *net)
671682
{
672683
struct mcp251x_priv *priv = netdev_priv(net);
673684
struct spi_device *spi = priv->spi;
674-
struct mcp251x_platform_data *pdata = spi->dev.platform_data;
675685

676686
free_irq(spi->irq, priv);
677687
mcp251x_hw_sleep(spi);
678-
if (pdata->transceiver_enable)
679-
pdata->transceiver_enable(0);
688+
mcp251x_power_enable(priv->transceiver, 0);
680689
close_candev(net);
681690
}
682691

683692
static int mcp251x_stop(struct net_device *net)
684693
{
685694
struct mcp251x_priv *priv = netdev_priv(net);
686695
struct spi_device *spi = priv->spi;
687-
struct mcp251x_platform_data *pdata = spi->dev.platform_data;
688696

689697
close_candev(net);
690698

@@ -704,8 +712,7 @@ static int mcp251x_stop(struct net_device *net)
704712

705713
mcp251x_hw_sleep(spi);
706714

707-
if (pdata->transceiver_enable)
708-
pdata->transceiver_enable(0);
715+
mcp251x_power_enable(priv->transceiver, 0);
709716

710717
priv->can.state = CAN_STATE_STOPPED;
711718

@@ -939,8 +946,7 @@ static int mcp251x_open(struct net_device *net)
939946
}
940947

941948
mutex_lock(&priv->mcp_lock);
942-
if (pdata->transceiver_enable)
943-
pdata->transceiver_enable(1);
949+
mcp251x_power_enable(priv->transceiver, 1);
944950

945951
priv->force_quit = 0;
946952
priv->tx_skb = NULL;
@@ -956,8 +962,7 @@ static int mcp251x_open(struct net_device *net)
956962
flags, DEVICE_NAME, priv);
957963
if (ret) {
958964
dev_err(&spi->dev, "failed to acquire irq %d\n", spi->irq);
959-
if (pdata->transceiver_enable)
960-
pdata->transceiver_enable(0);
965+
mcp251x_power_enable(priv->transceiver, 0);
961966
close_candev(net);
962967
goto open_unlock;
963968
}
@@ -1026,6 +1031,19 @@ static int mcp251x_can_probe(struct spi_device *spi)
10261031
CAN_CTRLMODE_LOOPBACK | CAN_CTRLMODE_LISTENONLY;
10271032
priv->model = spi_get_device_id(spi)->driver_data;
10281033
priv->net = net;
1034+
1035+
priv->power = devm_regulator_get(&spi->dev, "vdd");
1036+
priv->transceiver = devm_regulator_get(&spi->dev, "xceiver");
1037+
if ((PTR_ERR(priv->power) == -EPROBE_DEFER) ||
1038+
(PTR_ERR(priv->transceiver) == -EPROBE_DEFER)) {
1039+
ret = -EPROBE_DEFER;
1040+
goto error_power;
1041+
}
1042+
1043+
ret = mcp251x_power_enable(priv->power, 1);
1044+
if (ret)
1045+
goto error_power;
1046+
10291047
spi_set_drvdata(spi, priv);
10301048

10311049
priv->spi = spi;
@@ -1068,13 +1086,6 @@ static int mcp251x_can_probe(struct spi_device *spi)
10681086
}
10691087
}
10701088

1071-
if (pdata->power_enable)
1072-
pdata->power_enable(1);
1073-
1074-
/* Call out to platform specific setup */
1075-
if (pdata->board_specific_setup)
1076-
pdata->board_specific_setup(spi);
1077-
10781089
SET_NETDEV_DEV(net, &spi->dev);
10791090

10801091
/* Configure the SPI bus */
@@ -1084,14 +1095,11 @@ static int mcp251x_can_probe(struct spi_device *spi)
10841095

10851096
/* Here is OK to not lock the MCP, no one knows about it yet */
10861097
if (!mcp251x_hw_probe(spi)) {
1087-
dev_info(&spi->dev, "Probe failed\n");
1098+
ret = -ENODEV;
10881099
goto error_probe;
10891100
}
10901101
mcp251x_hw_sleep(spi);
10911102

1092-
if (pdata->transceiver_enable)
1093-
pdata->transceiver_enable(0);
1094-
10951103
ret = register_candev(net);
10961104
if (ret)
10971105
goto error_probe;
@@ -1109,26 +1117,24 @@ static int mcp251x_can_probe(struct spi_device *spi)
11091117
if (!mcp251x_enable_dma)
11101118
kfree(priv->spi_tx_buf);
11111119
error_tx_buf:
1112-
free_candev(net);
11131120
if (mcp251x_enable_dma)
11141121
dma_free_coherent(&spi->dev, PAGE_SIZE,
11151122
priv->spi_tx_buf, priv->spi_tx_dma);
1123+
mcp251x_power_enable(priv->power, 0);
1124+
error_power:
1125+
free_candev(net);
11161126
error_alloc:
1117-
if (pdata->power_enable)
1118-
pdata->power_enable(0);
11191127
dev_err(&spi->dev, "probe failed\n");
11201128
error_out:
11211129
return ret;
11221130
}
11231131

11241132
static int mcp251x_can_remove(struct spi_device *spi)
11251133
{
1126-
struct mcp251x_platform_data *pdata = spi->dev.platform_data;
11271134
struct mcp251x_priv *priv = spi_get_drvdata(spi);
11281135
struct net_device *net = priv->net;
11291136

11301137
unregister_candev(net);
1131-
free_candev(net);
11321138

11331139
if (mcp251x_enable_dma) {
11341140
dma_free_coherent(&spi->dev, PAGE_SIZE,
@@ -1138,8 +1144,9 @@ static int mcp251x_can_remove(struct spi_device *spi)
11381144
kfree(priv->spi_rx_buf);
11391145
}
11401146

1141-
if (pdata->power_enable)
1142-
pdata->power_enable(0);
1147+
mcp251x_power_enable(priv->power, 0);
1148+
1149+
free_candev(net);
11431150

11441151
return 0;
11451152
}
@@ -1149,7 +1156,6 @@ static int mcp251x_can_remove(struct spi_device *spi)
11491156
static int mcp251x_can_suspend(struct device *dev)
11501157
{
11511158
struct spi_device *spi = to_spi_device(dev);
1152-
struct mcp251x_platform_data *pdata = spi->dev.platform_data;
11531159
struct mcp251x_priv *priv = spi_get_drvdata(spi);
11541160
struct net_device *net = priv->net;
11551161

@@ -1163,15 +1169,14 @@ static int mcp251x_can_suspend(struct device *dev)
11631169
netif_device_detach(net);
11641170

11651171
mcp251x_hw_sleep(spi);
1166-
if (pdata->transceiver_enable)
1167-
pdata->transceiver_enable(0);
1172+
mcp251x_power_enable(priv->transceiver, 0);
11681173
priv->after_suspend = AFTER_SUSPEND_UP;
11691174
} else {
11701175
priv->after_suspend = AFTER_SUSPEND_DOWN;
11711176
}
11721177

1173-
if (pdata->power_enable) {
1174-
pdata->power_enable(0);
1178+
if (!IS_ERR(priv->power)) {
1179+
regulator_disable(priv->power);
11751180
priv->after_suspend |= AFTER_SUSPEND_POWER;
11761181
}
11771182

@@ -1181,16 +1186,14 @@ static int mcp251x_can_suspend(struct device *dev)
11811186
static int mcp251x_can_resume(struct device *dev)
11821187
{
11831188
struct spi_device *spi = to_spi_device(dev);
1184-
struct mcp251x_platform_data *pdata = spi->dev.platform_data;
11851189
struct mcp251x_priv *priv = spi_get_drvdata(spi);
11861190

11871191
if (priv->after_suspend & AFTER_SUSPEND_POWER) {
1188-
pdata->power_enable(1);
1192+
mcp251x_power_enable(priv->power, 1);
11891193
queue_work(priv->wq, &priv->restart_work);
11901194
} else {
11911195
if (priv->after_suspend & AFTER_SUSPEND_UP) {
1192-
if (pdata->transceiver_enable)
1193-
pdata->transceiver_enable(1);
1196+
mcp251x_power_enable(priv->transceiver, 1);
11941197
queue_work(priv->wq, &priv->restart_work);
11951198
} else {
11961199
priv->after_suspend = 0;

‎include/linux/can/platform/mcp251x.h‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,15 @@
99

1010
#include <linux/spi/spi.h>
1111

12-
/**
12+
/*
1313
* struct mcp251x_platform_data - MCP251X SPI CAN controller platform data
1414
* @oscillator_frequency: - oscillator frequency in Hz
1515
* @irq_flags: - IRQF configuration flags
16-
* @board_specific_setup: - called before probing the chip (power,reset)
17-
* @transceiver_enable: - called to power on/off the transceiver
18-
* @power_enable: - called to power on/off the mcp *and* the
19-
* transceiver
20-
*
21-
* Please note that you should define power_enable or transceiver_enable or
22-
* none of them. Defining both of them is no use.
23-
*
2416
*/
2517

2618
struct mcp251x_platform_data {
2719
unsigned long oscillator_frequency;
2820
unsigned long irq_flags;
29-
int (*board_specific_setup)(struct spi_device *spi);
30-
int (*transceiver_enable)(int enable);
31-
int (*power_enable) (int enable);
3221
};
3322

3423
#endif /* __CAN_PLATFORM_MCP251X_H__ */

0 commit comments

Comments
 (0)