]>
Commit | Line | Data |
---|---|---|
a0c7c1d4 MR |
1 | /* |
2 | * Copyright 2009-2010 Pengutronix | |
3 | * Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de> | |
4 | * | |
5 | * loosely based on an earlier driver that has | |
6 | * Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> | |
7 | * | |
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. | |
11 | */ | |
12 | ||
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> | |
20 | #include <linux/of.h> | |
21 | #include <linux/of_device.h> | |
22 | #include <linux/of_gpio.h> | |
23 | #include <linux/err.h> | |
24 | #include <linux/spi/spi.h> | |
25 | ||
26 | #include "mc13xxx.h" | |
27 | ||
28 | static const struct spi_device_id mc13xxx_device_id[] = { | |
29 | { | |
30 | .name = "mc13783", | |
cd0f34b0 | 31 | .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc13783, |
a0c7c1d4 MR |
32 | }, { |
33 | .name = "mc13892", | |
cd0f34b0 | 34 | .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc13892, |
a0c7c1d4 MR |
35 | }, { |
36 | /* sentinel */ | |
37 | } | |
38 | }; | |
39 | MODULE_DEVICE_TABLE(spi, mc13xxx_device_id); | |
40 | ||
41 | static const struct of_device_id mc13xxx_dt_ids[] = { | |
cd0f34b0 UKK |
42 | { .compatible = "fsl,mc13783", .data = &mc13xxx_variant_mc13783, }, |
43 | { .compatible = "fsl,mc13892", .data = &mc13xxx_variant_mc13892, }, | |
a0c7c1d4 MR |
44 | { /* sentinel */ } |
45 | }; | |
46 | MODULE_DEVICE_TABLE(of, mc13xxx_dt_ids); | |
47 | ||
48 | static struct regmap_config mc13xxx_regmap_spi_config = { | |
49 | .reg_bits = 7, | |
50 | .pad_bits = 1, | |
51 | .val_bits = 24, | |
77a5b370 | 52 | .write_flag_mask = 0x80, |
a0c7c1d4 MR |
53 | |
54 | .max_register = MC13XXX_NUMREGS, | |
55 | ||
56 | .cache_type = REGCACHE_NONE, | |
e4ecf6ea PR |
57 | .use_single_rw = 1, |
58 | }; | |
59 | ||
60 | static int mc13xxx_spi_read(void *context, const void *reg, size_t reg_size, | |
61 | void *val, size_t val_size) | |
62 | { | |
63 | unsigned char w[4] = { *((unsigned char *) reg), 0, 0, 0}; | |
64 | unsigned char r[4]; | |
65 | unsigned char *p = val; | |
66 | struct device *dev = context; | |
67 | struct spi_device *spi = to_spi_device(dev); | |
68 | struct spi_transfer t = { | |
69 | .tx_buf = w, | |
70 | .rx_buf = r, | |
71 | .len = 4, | |
72 | }; | |
73 | ||
74 | struct spi_message m; | |
75 | int ret; | |
76 | ||
77 | if (val_size != 3 || reg_size != 1) | |
78 | return -ENOTSUPP; | |
79 | ||
80 | spi_message_init(&m); | |
81 | spi_message_add_tail(&t, &m); | |
82 | ret = spi_sync(spi, &m); | |
83 | ||
84 | memcpy(p, &r[1], 3); | |
85 | ||
86 | return ret; | |
87 | } | |
88 | ||
89 | static int mc13xxx_spi_write(void *context, const void *data, size_t count) | |
90 | { | |
91 | struct device *dev = context; | |
92 | struct spi_device *spi = to_spi_device(dev); | |
93 | ||
94 | if (count != 4) | |
95 | return -ENOTSUPP; | |
96 | ||
97 | return spi_write(spi, data, count); | |
98 | } | |
99 | ||
100 | /* | |
101 | * We cannot use regmap-spi generic bus implementation here. | |
102 | * The MC13783 chip will get corrupted if CS signal is deasserted | |
103 | * and on i.Mx31 SoC (the target SoC for MC13783 PMIC) the SPI controller | |
104 | * has the following errata (DSPhl22960): | |
105 | * "The CSPI negates SS when the FIFO becomes empty with | |
106 | * SSCTL= 0. Software cannot guarantee that the FIFO will not | |
107 | * drain because of higher priority interrupts and the | |
108 | * non-realtime characteristics of the operating system. As a | |
109 | * result, the SS will negate before all of the data has been | |
110 | * transferred to/from the peripheral." | |
111 | * We workaround this by accessing the SPI controller with a | |
112 | * single transfert. | |
113 | */ | |
114 | ||
115 | static struct regmap_bus regmap_mc13xxx_bus = { | |
116 | .write = mc13xxx_spi_write, | |
117 | .read = mc13xxx_spi_read, | |
a0c7c1d4 MR |
118 | }; |
119 | ||
120 | static int mc13xxx_spi_probe(struct spi_device *spi) | |
121 | { | |
a0c7c1d4 MR |
122 | struct mc13xxx *mc13xxx; |
123 | struct mc13xxx_platform_data *pdata = dev_get_platdata(&spi->dev); | |
124 | int ret; | |
125 | ||
e7c706b1 | 126 | mc13xxx = devm_kzalloc(&spi->dev, sizeof(*mc13xxx), GFP_KERNEL); |
a0c7c1d4 MR |
127 | if (!mc13xxx) |
128 | return -ENOMEM; | |
129 | ||
130 | dev_set_drvdata(&spi->dev, mc13xxx); | |
131 | spi->mode = SPI_MODE_0 | SPI_CS_HIGH; | |
a0c7c1d4 MR |
132 | |
133 | mc13xxx->dev = &spi->dev; | |
134 | mutex_init(&mc13xxx->lock); | |
135 | ||
e0308897 AL |
136 | mc13xxx->regmap = devm_regmap_init(&spi->dev, ®map_mc13xxx_bus, |
137 | &spi->dev, | |
138 | &mc13xxx_regmap_spi_config); | |
a0c7c1d4 MR |
139 | if (IS_ERR(mc13xxx->regmap)) { |
140 | ret = PTR_ERR(mc13xxx->regmap); | |
141 | dev_err(mc13xxx->dev, "Failed to initialize register map: %d\n", | |
142 | ret); | |
143 | dev_set_drvdata(&spi->dev, NULL); | |
a0c7c1d4 MR |
144 | return ret; |
145 | } | |
146 | ||
cd0f34b0 UKK |
147 | if (spi->dev.of_node) { |
148 | const struct of_device_id *of_id = | |
149 | of_match_device(mc13xxx_dt_ids, &spi->dev); | |
a0c7c1d4 | 150 | |
cd0f34b0 | 151 | mc13xxx->variant = of_id->data; |
a0c7c1d4 | 152 | } else { |
cd0f34b0 UKK |
153 | const struct spi_device_id *id_entry = spi_get_device_id(spi); |
154 | ||
155 | mc13xxx->variant = (void *)id_entry->driver_data; | |
a0c7c1d4 MR |
156 | } |
157 | ||
cd0f34b0 | 158 | return mc13xxx_common_init(mc13xxx, pdata, spi->irq); |
a0c7c1d4 MR |
159 | } |
160 | ||
161 | static int __devexit mc13xxx_spi_remove(struct spi_device *spi) | |
162 | { | |
163 | struct mc13xxx *mc13xxx = dev_get_drvdata(&spi->dev); | |
164 | ||
165 | mc13xxx_common_cleanup(mc13xxx); | |
166 | ||
167 | return 0; | |
168 | } | |
169 | ||
170 | static struct spi_driver mc13xxx_spi_driver = { | |
171 | .id_table = mc13xxx_device_id, | |
172 | .driver = { | |
173 | .name = "mc13xxx", | |
174 | .owner = THIS_MODULE, | |
175 | .of_match_table = mc13xxx_dt_ids, | |
176 | }, | |
177 | .probe = mc13xxx_spi_probe, | |
178 | .remove = __devexit_p(mc13xxx_spi_remove), | |
179 | }; | |
180 | ||
181 | static int __init mc13xxx_init(void) | |
182 | { | |
183 | return spi_register_driver(&mc13xxx_spi_driver); | |
184 | } | |
185 | subsys_initcall(mc13xxx_init); | |
186 | ||
187 | static void __exit mc13xxx_exit(void) | |
188 | { | |
189 | spi_unregister_driver(&mc13xxx_spi_driver); | |
190 | } | |
191 | module_exit(mc13xxx_exit); | |
192 | ||
193 | MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC"); | |
194 | MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>"); | |
195 | MODULE_LICENSE("GPL v2"); |