1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2009-2010 Pengutronix
4 * Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>
6 * loosely based on an earlier driver that has
7 * Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
10 #include <linux/slab.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/interrupt.h>
15 #include <linux/mfd/core.h>
16 #include <linux/mfd/mc13xxx.h>
17 #include <linux/err.h>
18 #include <linux/spi/spi.h>
22 static const struct spi_device_id mc13xxx_device_id
[] = {
25 .driver_data
= (kernel_ulong_t
)&mc13xxx_variant_mc13783
,
28 .driver_data
= (kernel_ulong_t
)&mc13xxx_variant_mc13892
,
31 .driver_data
= (kernel_ulong_t
)&mc13xxx_variant_mc34708
,
36 MODULE_DEVICE_TABLE(spi
, mc13xxx_device_id
);
38 static const struct of_device_id mc13xxx_dt_ids
[] = {
39 { .compatible
= "fsl,mc13783", .data
= &mc13xxx_variant_mc13783
, },
40 { .compatible
= "fsl,mc13892", .data
= &mc13xxx_variant_mc13892
, },
41 { .compatible
= "fsl,mc34708", .data
= &mc13xxx_variant_mc34708
, },
44 MODULE_DEVICE_TABLE(of
, mc13xxx_dt_ids
);
46 static const struct regmap_config mc13xxx_regmap_spi_config
= {
50 .write_flag_mask
= 0x80,
52 .max_register
= MC13XXX_NUMREGS
,
54 .cache_type
= REGCACHE_NONE
,
55 .use_single_read
= true,
56 .use_single_write
= true,
59 static int mc13xxx_spi_read(void *context
, const void *reg
, size_t reg_size
,
60 void *val
, size_t val_size
)
62 unsigned char w
[4] = { *((unsigned char *) reg
), 0, 0, 0};
64 unsigned char *p
= val
;
65 struct device
*dev
= context
;
66 struct spi_device
*spi
= to_spi_device(dev
);
67 struct spi_transfer t
= {
76 if (val_size
!= 3 || reg_size
!= 1)
80 spi_message_add_tail(&t
, &m
);
81 ret
= spi_sync(spi
, &m
);
88 static int mc13xxx_spi_write(void *context
, const void *data
, size_t count
)
90 struct device
*dev
= context
;
91 struct spi_device
*spi
= to_spi_device(dev
);
92 const char *reg
= data
;
97 /* include errata fix for spi audio problems */
98 if (*reg
== MC13783_AUDIO_CODEC
|| *reg
== MC13783_AUDIO_DAC
)
99 spi_write(spi
, data
, count
);
101 return spi_write(spi
, data
, count
);
105 * We cannot use regmap-spi generic bus implementation here.
106 * The MC13783 chip will get corrupted if CS signal is deasserted
107 * and on i.Mx31 SoC (the target SoC for MC13783 PMIC) the SPI controller
108 * has the following errata (DSPhl22960):
109 * "The CSPI negates SS when the FIFO becomes empty with
110 * SSCTL= 0. Software cannot guarantee that the FIFO will not
111 * drain because of higher priority interrupts and the
112 * non-realtime characteristics of the operating system. As a
113 * result, the SS will negate before all of the data has been
114 * transferred to/from the peripheral."
115 * We workaround this by accessing the SPI controller with a
119 static const struct regmap_bus regmap_mc13xxx_bus
= {
120 .write
= mc13xxx_spi_write
,
121 .read
= mc13xxx_spi_read
,
124 static int mc13xxx_spi_probe(struct spi_device
*spi
)
126 struct mc13xxx
*mc13xxx
;
129 mc13xxx
= devm_kzalloc(&spi
->dev
, sizeof(*mc13xxx
), GFP_KERNEL
);
133 dev_set_drvdata(&spi
->dev
, mc13xxx
);
135 spi
->mode
= SPI_MODE_0
| SPI_CS_HIGH
;
137 mc13xxx
->irq
= spi
->irq
;
139 spi
->max_speed_hz
= spi
->max_speed_hz
? : 26000000;
140 ret
= spi_setup(spi
);
144 mc13xxx
->regmap
= devm_regmap_init(&spi
->dev
, ®map_mc13xxx_bus
,
146 &mc13xxx_regmap_spi_config
);
147 if (IS_ERR(mc13xxx
->regmap
)) {
148 ret
= PTR_ERR(mc13xxx
->regmap
);
149 dev_err(&spi
->dev
, "Failed to initialize regmap: %d\n", ret
);
153 mc13xxx
->variant
= spi_get_device_match_data(spi
);
155 return mc13xxx_common_init(&spi
->dev
);
158 static void mc13xxx_spi_remove(struct spi_device
*spi
)
160 mc13xxx_common_exit(&spi
->dev
);
163 static struct spi_driver mc13xxx_spi_driver
= {
164 .id_table
= mc13xxx_device_id
,
167 .of_match_table
= mc13xxx_dt_ids
,
169 .probe
= mc13xxx_spi_probe
,
170 .remove
= mc13xxx_spi_remove
,
173 static int __init
mc13xxx_init(void)
175 return spi_register_driver(&mc13xxx_spi_driver
);
177 subsys_initcall(mc13xxx_init
);
179 static void __exit
mc13xxx_exit(void)
181 spi_unregister_driver(&mc13xxx_spi_driver
);
183 module_exit(mc13xxx_exit
);
185 MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC");
186 MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
187 MODULE_LICENSE("GPL v2");