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/mutex.h>
17 #include <linux/interrupt.h>
18 #include <linux/mfd/core.h>
19 #include <linux/mfd/mc13xxx.h>
21 #include <linux/of_device.h>
22 #include <linux/of_gpio.h>
23 #include <linux/err.h>
24 #include <linux/spi/spi.h>
28 static const struct spi_device_id mc13xxx_device_id
[] = {
31 .driver_data
= (kernel_ulong_t
)&mc13xxx_variant_mc13783
,
34 .driver_data
= (kernel_ulong_t
)&mc13xxx_variant_mc13892
,
37 .driver_data
= (kernel_ulong_t
)&mc13xxx_variant_mc34708
,
42 MODULE_DEVICE_TABLE(spi
, mc13xxx_device_id
);
44 static const struct of_device_id mc13xxx_dt_ids
[] = {
45 { .compatible
= "fsl,mc13783", .data
= &mc13xxx_variant_mc13783
, },
46 { .compatible
= "fsl,mc13892", .data
= &mc13xxx_variant_mc13892
, },
47 { .compatible
= "fsl,mc34708", .data
= &mc13xxx_variant_mc34708
, },
50 MODULE_DEVICE_TABLE(of
, mc13xxx_dt_ids
);
52 static struct regmap_config mc13xxx_regmap_spi_config
= {
56 .write_flag_mask
= 0x80,
58 .max_register
= MC13XXX_NUMREGS
,
60 .cache_type
= REGCACHE_NONE
,
64 static int mc13xxx_spi_read(void *context
, const void *reg
, size_t reg_size
,
65 void *val
, size_t val_size
)
67 unsigned char w
[4] = { *((unsigned char *) reg
), 0, 0, 0};
69 unsigned char *p
= val
;
70 struct device
*dev
= context
;
71 struct spi_device
*spi
= to_spi_device(dev
);
72 struct spi_transfer t
= {
81 if (val_size
!= 3 || reg_size
!= 1)
85 spi_message_add_tail(&t
, &m
);
86 ret
= spi_sync(spi
, &m
);
93 static int mc13xxx_spi_write(void *context
, const void *data
, size_t count
)
95 struct device
*dev
= context
;
96 struct spi_device
*spi
= to_spi_device(dev
);
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 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
;
127 struct mc13xxx_platform_data
*pdata
= dev_get_platdata(&spi
->dev
);
130 mc13xxx
= devm_kzalloc(&spi
->dev
, sizeof(*mc13xxx
), GFP_KERNEL
);
134 spi_set_drvdata(spi
, mc13xxx
);
135 spi
->mode
= SPI_MODE_0
| SPI_CS_HIGH
;
137 mc13xxx
->dev
= &spi
->dev
;
138 mutex_init(&mc13xxx
->lock
);
140 mc13xxx
->regmap
= devm_regmap_init(&spi
->dev
, ®map_mc13xxx_bus
,
142 &mc13xxx_regmap_spi_config
);
143 if (IS_ERR(mc13xxx
->regmap
)) {
144 ret
= PTR_ERR(mc13xxx
->regmap
);
145 dev_err(mc13xxx
->dev
, "Failed to initialize register map: %d\n",
147 spi_set_drvdata(spi
, NULL
);
151 if (spi
->dev
.of_node
) {
152 const struct of_device_id
*of_id
=
153 of_match_device(mc13xxx_dt_ids
, &spi
->dev
);
155 mc13xxx
->variant
= of_id
->data
;
157 const struct spi_device_id
*id_entry
= spi_get_device_id(spi
);
159 mc13xxx
->variant
= (void *)id_entry
->driver_data
;
162 return mc13xxx_common_init(mc13xxx
, pdata
, spi
->irq
);
165 static int mc13xxx_spi_remove(struct spi_device
*spi
)
167 struct mc13xxx
*mc13xxx
= spi_get_drvdata(spi
);
169 mc13xxx_common_cleanup(mc13xxx
);
174 static struct spi_driver mc13xxx_spi_driver
= {
175 .id_table
= mc13xxx_device_id
,
178 .owner
= THIS_MODULE
,
179 .of_match_table
= mc13xxx_dt_ids
,
181 .probe
= mc13xxx_spi_probe
,
182 .remove
= mc13xxx_spi_remove
,
185 static int __init
mc13xxx_init(void)
187 return spi_register_driver(&mc13xxx_spi_driver
);
189 subsys_initcall(mc13xxx_init
);
191 static void __exit
mc13xxx_exit(void)
193 spi_unregister_driver(&mc13xxx_spi_driver
);
195 module_exit(mc13xxx_exit
);
197 MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC");
198 MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
199 MODULE_LICENSE("GPL v2");