2 * Copyright 2009-2010 Pengutronix
3 * Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>
5 * loosely based on an earlier driver that has
6 * Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License version 2 as published by the
10 * Free Software Foundation.
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/interrupt.h>
17 #include <linux/mfd/core.h>
18 #include <linux/mfd/mc13xxx.h>
20 #include <linux/of_device.h>
21 #include <linux/of_gpio.h>
22 #include <linux/err.h>
23 #include <linux/spi/spi.h>
27 static const struct spi_device_id mc13xxx_device_id
[] = {
30 .driver_data
= (kernel_ulong_t
)&mc13xxx_variant_mc13783
,
33 .driver_data
= (kernel_ulong_t
)&mc13xxx_variant_mc13892
,
36 .driver_data
= (kernel_ulong_t
)&mc13xxx_variant_mc34708
,
41 MODULE_DEVICE_TABLE(spi
, mc13xxx_device_id
);
43 static const struct of_device_id mc13xxx_dt_ids
[] = {
44 { .compatible
= "fsl,mc13783", .data
= &mc13xxx_variant_mc13783
, },
45 { .compatible
= "fsl,mc13892", .data
= &mc13xxx_variant_mc13892
, },
46 { .compatible
= "fsl,mc34708", .data
= &mc13xxx_variant_mc34708
, },
49 MODULE_DEVICE_TABLE(of
, mc13xxx_dt_ids
);
51 static struct regmap_config mc13xxx_regmap_spi_config
= {
55 .write_flag_mask
= 0x80,
57 .max_register
= MC13XXX_NUMREGS
,
59 .cache_type
= REGCACHE_NONE
,
63 static int mc13xxx_spi_read(void *context
, const void *reg
, size_t reg_size
,
64 void *val
, size_t val_size
)
66 unsigned char w
[4] = { *((unsigned char *) reg
), 0, 0, 0};
68 unsigned char *p
= val
;
69 struct device
*dev
= context
;
70 struct spi_device
*spi
= to_spi_device(dev
);
71 struct spi_transfer t
= {
80 if (val_size
!= 3 || reg_size
!= 1)
84 spi_message_add_tail(&t
, &m
);
85 ret
= spi_sync(spi
, &m
);
92 static int mc13xxx_spi_write(void *context
, const void *data
, size_t count
)
94 struct device
*dev
= context
;
95 struct spi_device
*spi
= to_spi_device(dev
);
96 const char *reg
= data
;
101 /* include errata fix for spi audio problems */
102 if (*reg
== MC13783_AUDIO_CODEC
|| *reg
== MC13783_AUDIO_DAC
)
103 spi_write(spi
, data
, count
);
105 return spi_write(spi
, data
, count
);
109 * We cannot use regmap-spi generic bus implementation here.
110 * The MC13783 chip will get corrupted if CS signal is deasserted
111 * and on i.Mx31 SoC (the target SoC for MC13783 PMIC) the SPI controller
112 * has the following errata (DSPhl22960):
113 * "The CSPI negates SS when the FIFO becomes empty with
114 * SSCTL= 0. Software cannot guarantee that the FIFO will not
115 * drain because of higher priority interrupts and the
116 * non-realtime characteristics of the operating system. As a
117 * result, the SS will negate before all of the data has been
118 * transferred to/from the peripheral."
119 * We workaround this by accessing the SPI controller with a
123 static struct regmap_bus regmap_mc13xxx_bus
= {
124 .write
= mc13xxx_spi_write
,
125 .read
= mc13xxx_spi_read
,
128 static int mc13xxx_spi_probe(struct spi_device
*spi
)
130 struct mc13xxx
*mc13xxx
;
133 mc13xxx
= devm_kzalloc(&spi
->dev
, sizeof(*mc13xxx
), GFP_KERNEL
);
137 dev_set_drvdata(&spi
->dev
, mc13xxx
);
139 spi
->mode
= SPI_MODE_0
| SPI_CS_HIGH
;
141 mc13xxx
->irq
= spi
->irq
;
143 mc13xxx
->regmap
= devm_regmap_init(&spi
->dev
, ®map_mc13xxx_bus
,
145 &mc13xxx_regmap_spi_config
);
146 if (IS_ERR(mc13xxx
->regmap
)) {
147 ret
= PTR_ERR(mc13xxx
->regmap
);
148 dev_err(&spi
->dev
, "Failed to initialize regmap: %d\n", ret
);
152 if (spi
->dev
.of_node
) {
153 const struct of_device_id
*of_id
=
154 of_match_device(mc13xxx_dt_ids
, &spi
->dev
);
156 mc13xxx
->variant
= of_id
->data
;
158 const struct spi_device_id
*id_entry
= spi_get_device_id(spi
);
160 mc13xxx
->variant
= (void *)id_entry
->driver_data
;
163 return mc13xxx_common_init(&spi
->dev
);
166 static int mc13xxx_spi_remove(struct spi_device
*spi
)
168 return mc13xxx_common_exit(&spi
->dev
);
171 static struct spi_driver mc13xxx_spi_driver
= {
172 .id_table
= mc13xxx_device_id
,
175 .owner
= THIS_MODULE
,
176 .of_match_table
= mc13xxx_dt_ids
,
178 .probe
= mc13xxx_spi_probe
,
179 .remove
= mc13xxx_spi_remove
,
182 static int __init
mc13xxx_init(void)
184 return spi_register_driver(&mc13xxx_spi_driver
);
186 subsys_initcall(mc13xxx_init
);
188 static void __exit
mc13xxx_exit(void)
190 spi_unregister_driver(&mc13xxx_spi_driver
);
192 module_exit(mc13xxx_exit
);
194 MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC");
195 MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
196 MODULE_LICENSE("GPL v2");