Revert "unicode: Don't special case ignorable code points"
[linux.git] / drivers / mfd / ipaq-micro.c
blob2370b44e2214489ac305402f3ea097da47c3b014
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Compaq iPAQ h3xxx Atmel microcontroller companion support
5 * This is an Atmel AT90LS8535 with a special flashed-in firmware that
6 * implements the special protocol used by this driver.
8 * based on previous kernel 2.4 version by Andrew Christian
9 * Author : Alessandro Gardich <gremlin@gremlin.it>
10 * Author : Dmitry Artamonow <mad_soft@inbox.ru>
11 * Author : Linus Walleij <linus.walleij@linaro.org>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/pm.h>
18 #include <linux/delay.h>
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/io.h>
22 #include <linux/mfd/core.h>
23 #include <linux/mfd/ipaq-micro.h>
24 #include <linux/string.h>
25 #include <linux/random.h>
26 #include <linux/slab.h>
27 #include <linux/list.h>
29 #include <mach/hardware.h>
31 static void ipaq_micro_trigger_tx(struct ipaq_micro *micro)
33 struct ipaq_micro_txdev *tx = &micro->tx;
34 struct ipaq_micro_msg *msg = micro->msg;
35 int i, bp;
36 u8 checksum;
37 u32 val;
39 bp = 0;
40 tx->buf[bp++] = CHAR_SOF;
42 checksum = ((msg->id & 0x0f) << 4) | (msg->tx_len & 0x0f);
43 tx->buf[bp++] = checksum;
45 for (i = 0; i < msg->tx_len; i++) {
46 tx->buf[bp++] = msg->tx_data[i];
47 checksum += msg->tx_data[i];
50 tx->buf[bp++] = checksum;
51 tx->len = bp;
52 tx->index = 0;
54 /* Enable interrupt */
55 val = readl(micro->base + UTCR3);
56 val |= UTCR3_TIE;
57 writel(val, micro->base + UTCR3);
60 int ipaq_micro_tx_msg(struct ipaq_micro *micro, struct ipaq_micro_msg *msg)
62 unsigned long flags;
64 dev_dbg(micro->dev, "TX msg: %02x, %d bytes\n", msg->id, msg->tx_len);
66 spin_lock_irqsave(&micro->lock, flags);
67 if (micro->msg) {
68 list_add_tail(&msg->node, &micro->queue);
69 spin_unlock_irqrestore(&micro->lock, flags);
70 return 0;
72 micro->msg = msg;
73 ipaq_micro_trigger_tx(micro);
74 spin_unlock_irqrestore(&micro->lock, flags);
75 return 0;
77 EXPORT_SYMBOL(ipaq_micro_tx_msg);
79 static void micro_rx_msg(struct ipaq_micro *micro, u8 id, int len, u8 *data)
81 dev_dbg(micro->dev, "RX msg: %02x, %d bytes\n", id, len);
83 spin_lock(&micro->lock);
84 switch (id) {
85 case MSG_VERSION:
86 case MSG_EEPROM_READ:
87 case MSG_EEPROM_WRITE:
88 case MSG_BACKLIGHT:
89 case MSG_NOTIFY_LED:
90 case MSG_THERMAL_SENSOR:
91 case MSG_BATTERY:
92 /* Handle synchronous messages */
93 if (micro->msg && micro->msg->id == id) {
94 struct ipaq_micro_msg *msg = micro->msg;
96 memcpy(msg->rx_data, data, len);
97 msg->rx_len = len;
98 complete(&micro->msg->ack);
99 if (!list_empty(&micro->queue)) {
100 micro->msg = list_entry(micro->queue.next,
101 struct ipaq_micro_msg,
102 node);
103 list_del_init(&micro->msg->node);
104 ipaq_micro_trigger_tx(micro);
105 } else
106 micro->msg = NULL;
107 dev_dbg(micro->dev, "OK RX message 0x%02x\n", id);
108 } else {
109 dev_err(micro->dev,
110 "out of band RX message 0x%02x\n", id);
111 if (!micro->msg)
112 dev_info(micro->dev, "no message queued\n");
113 else
114 dev_info(micro->dev, "expected message %02x\n",
115 micro->msg->id);
117 break;
118 case MSG_KEYBOARD:
119 if (micro->key)
120 micro->key(micro->key_data, len, data);
121 else
122 dev_dbg(micro->dev, "key message ignored, no handle\n");
123 break;
124 case MSG_TOUCHSCREEN:
125 if (micro->ts)
126 micro->ts(micro->ts_data, len, data);
127 else
128 dev_dbg(micro->dev, "touchscreen message ignored, no handle\n");
129 break;
130 default:
131 dev_err(micro->dev,
132 "unknown msg %d [%d] %*ph\n", id, len, len, data);
133 break;
135 spin_unlock(&micro->lock);
138 static void micro_process_char(struct ipaq_micro *micro, u8 ch)
140 struct ipaq_micro_rxdev *rx = &micro->rx;
142 switch (rx->state) {
143 case STATE_SOF: /* Looking for SOF */
144 if (ch == CHAR_SOF)
145 rx->state = STATE_ID; /* Next byte is the id and len */
146 break;
147 case STATE_ID: /* Looking for id and len byte */
148 rx->id = (ch & 0xf0) >> 4;
149 rx->len = (ch & 0x0f);
150 rx->index = 0;
151 rx->chksum = ch;
152 rx->state = (rx->len > 0) ? STATE_DATA : STATE_CHKSUM;
153 break;
154 case STATE_DATA: /* Looking for 'len' data bytes */
155 rx->chksum += ch;
156 rx->buf[rx->index] = ch;
157 if (++rx->index == rx->len)
158 rx->state = STATE_CHKSUM;
159 break;
160 case STATE_CHKSUM: /* Looking for the checksum */
161 if (ch == rx->chksum)
162 micro_rx_msg(micro, rx->id, rx->len, rx->buf);
163 rx->state = STATE_SOF;
164 break;
168 static void micro_rx_chars(struct ipaq_micro *micro)
170 u32 status, ch;
172 while ((status = readl(micro->base + UTSR1)) & UTSR1_RNE) {
173 ch = readl(micro->base + UTDR);
174 if (status & UTSR1_PRE)
175 dev_err(micro->dev, "rx: parity error\n");
176 else if (status & UTSR1_FRE)
177 dev_err(micro->dev, "rx: framing error\n");
178 else if (status & UTSR1_ROR)
179 dev_err(micro->dev, "rx: overrun error\n");
180 micro_process_char(micro, ch);
184 static void ipaq_micro_get_version(struct ipaq_micro *micro)
186 struct ipaq_micro_msg msg = {
187 .id = MSG_VERSION,
190 ipaq_micro_tx_msg_sync(micro, &msg);
191 if (msg.rx_len == 4) {
192 memcpy(micro->version, msg.rx_data, 4);
193 micro->version[4] = '\0';
194 } else if (msg.rx_len == 9) {
195 memcpy(micro->version, msg.rx_data, 4);
196 micro->version[4] = '\0';
197 /* Bytes 4-7 are "pack", byte 8 is "boot type" */
198 } else {
199 dev_err(micro->dev,
200 "illegal version message %d bytes\n", msg.rx_len);
204 static void ipaq_micro_eeprom_read(struct ipaq_micro *micro,
205 u8 address, u8 len, u8 *data)
207 struct ipaq_micro_msg msg = {
208 .id = MSG_EEPROM_READ,
210 u8 i;
212 for (i = 0; i < len; i++) {
213 msg.tx_data[0] = address + i;
214 msg.tx_data[1] = 1;
215 msg.tx_len = 2;
216 ipaq_micro_tx_msg_sync(micro, &msg);
217 memcpy(data + (i * 2), msg.rx_data, 2);
221 static char *ipaq_micro_str(u8 *wchar, u8 len)
223 char retstr[256];
224 u8 i;
226 for (i = 0; i < len / 2; i++)
227 retstr[i] = wchar[i * 2];
228 return kstrdup(retstr, GFP_KERNEL);
231 static u16 ipaq_micro_to_u16(u8 *data)
233 return data[1] << 8 | data[0];
236 static void __init ipaq_micro_eeprom_dump(struct ipaq_micro *micro)
238 u8 dump[256];
239 char *str;
241 ipaq_micro_eeprom_read(micro, 0, 128, dump);
242 str = ipaq_micro_str(dump, 10);
243 if (str) {
244 dev_info(micro->dev, "HW version %s\n", str);
245 kfree(str);
247 str = ipaq_micro_str(dump+10, 40);
248 if (str) {
249 dev_info(micro->dev, "serial number: %s\n", str);
250 /* Feed the random pool with this */
251 add_device_randomness(str, strlen(str));
252 kfree(str);
254 str = ipaq_micro_str(dump+50, 20);
255 if (str) {
256 dev_info(micro->dev, "module ID: %s\n", str);
257 kfree(str);
259 str = ipaq_micro_str(dump+70, 10);
260 if (str) {
261 dev_info(micro->dev, "product revision: %s\n", str);
262 kfree(str);
264 dev_info(micro->dev, "product ID: %u\n", ipaq_micro_to_u16(dump+80));
265 dev_info(micro->dev, "frame rate: %u fps\n",
266 ipaq_micro_to_u16(dump+82));
267 dev_info(micro->dev, "page mode: %u\n", ipaq_micro_to_u16(dump+84));
268 dev_info(micro->dev, "country ID: %u\n", ipaq_micro_to_u16(dump+86));
269 dev_info(micro->dev, "color display: %s\n",
270 ipaq_micro_to_u16(dump+88) ? "yes" : "no");
271 dev_info(micro->dev, "ROM size: %u MiB\n", ipaq_micro_to_u16(dump+90));
272 dev_info(micro->dev, "RAM size: %u KiB\n", ipaq_micro_to_u16(dump+92));
273 dev_info(micro->dev, "screen: %u x %u\n",
274 ipaq_micro_to_u16(dump+94), ipaq_micro_to_u16(dump+96));
277 static void micro_tx_chars(struct ipaq_micro *micro)
279 struct ipaq_micro_txdev *tx = &micro->tx;
280 u32 val;
282 while ((tx->index < tx->len) &&
283 (readl(micro->base + UTSR1) & UTSR1_TNF)) {
284 writel(tx->buf[tx->index], micro->base + UTDR);
285 tx->index++;
288 /* Stop interrupts */
289 val = readl(micro->base + UTCR3);
290 val &= ~UTCR3_TIE;
291 writel(val, micro->base + UTCR3);
294 static void micro_reset_comm(struct ipaq_micro *micro)
296 struct ipaq_micro_rxdev *rx = &micro->rx;
297 u32 val;
299 if (micro->msg)
300 complete(&micro->msg->ack);
302 /* Initialize Serial channel protocol frame */
303 rx->state = STATE_SOF; /* Reset the state machine */
305 /* Set up interrupts */
306 writel(0x01, micro->sdlc + 0x0); /* Select UART mode */
308 /* Clean up CR3 */
309 writel(0x0, micro->base + UTCR3);
311 /* Format: 8N1 */
312 writel(UTCR0_8BitData | UTCR0_1StpBit, micro->base + UTCR0);
314 /* Baud rate: 115200 */
315 writel(0x0, micro->base + UTCR1);
316 writel(0x1, micro->base + UTCR2);
318 /* Clear SR0 */
319 writel(0xff, micro->base + UTSR0);
321 /* Enable RX int, disable TX int */
322 writel(UTCR3_TXE | UTCR3_RXE | UTCR3_RIE, micro->base + UTCR3);
323 val = readl(micro->base + UTCR3);
324 val &= ~UTCR3_TIE;
325 writel(val, micro->base + UTCR3);
328 static irqreturn_t micro_serial_isr(int irq, void *dev_id)
330 struct ipaq_micro *micro = dev_id;
331 struct ipaq_micro_txdev *tx = &micro->tx;
332 u32 status;
334 status = readl(micro->base + UTSR0);
335 do {
336 if (status & (UTSR0_RID | UTSR0_RFS)) {
337 if (status & UTSR0_RID)
338 /* Clear the Receiver IDLE bit */
339 writel(UTSR0_RID, micro->base + UTSR0);
340 micro_rx_chars(micro);
343 /* Clear break bits */
344 if (status & (UTSR0_RBB | UTSR0_REB))
345 writel(status & (UTSR0_RBB | UTSR0_REB),
346 micro->base + UTSR0);
348 if (status & UTSR0_TFS)
349 micro_tx_chars(micro);
351 status = readl(micro->base + UTSR0);
353 } while (((tx->index < tx->len) && (status & UTSR0_TFS)) ||
354 (status & (UTSR0_RFS | UTSR0_RID)));
356 return IRQ_HANDLED;
359 static const struct mfd_cell micro_cells[] = {
360 { .name = "ipaq-micro-backlight", },
361 { .name = "ipaq-micro-battery", },
362 { .name = "ipaq-micro-keys", },
363 { .name = "ipaq-micro-ts", },
364 { .name = "ipaq-micro-leds", },
367 static int __maybe_unused micro_resume(struct device *dev)
369 struct ipaq_micro *micro = dev_get_drvdata(dev);
371 micro_reset_comm(micro);
372 mdelay(10);
374 return 0;
377 static int __init micro_probe(struct platform_device *pdev)
379 struct ipaq_micro *micro;
380 int ret;
381 int irq;
383 micro = devm_kzalloc(&pdev->dev, sizeof(*micro), GFP_KERNEL);
384 if (!micro)
385 return -ENOMEM;
387 micro->dev = &pdev->dev;
389 micro->base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
390 if (IS_ERR(micro->base))
391 return PTR_ERR(micro->base);
393 micro->sdlc = devm_platform_ioremap_resource(pdev, 1);
394 if (IS_ERR(micro->sdlc))
395 return PTR_ERR(micro->sdlc);
397 micro_reset_comm(micro);
399 irq = platform_get_irq(pdev, 0);
400 if (irq < 0)
401 return -EINVAL;
402 ret = devm_request_irq(&pdev->dev, irq, micro_serial_isr,
403 IRQF_SHARED, "ipaq-micro",
404 micro);
405 if (ret) {
406 dev_err(&pdev->dev, "unable to grab serial port IRQ\n");
407 return ret;
408 } else
409 dev_info(&pdev->dev, "grabbed serial port IRQ\n");
411 spin_lock_init(&micro->lock);
412 INIT_LIST_HEAD(&micro->queue);
413 platform_set_drvdata(pdev, micro);
415 ret = mfd_add_devices(&pdev->dev, pdev->id, micro_cells,
416 ARRAY_SIZE(micro_cells), NULL, 0, NULL);
417 if (ret) {
418 dev_err(&pdev->dev, "error adding MFD cells");
419 return ret;
422 /* Check version */
423 ipaq_micro_get_version(micro);
424 dev_info(&pdev->dev, "Atmel micro ASIC version %s\n", micro->version);
425 ipaq_micro_eeprom_dump(micro);
427 return 0;
430 static const struct dev_pm_ops micro_dev_pm_ops = {
431 SET_SYSTEM_SLEEP_PM_OPS(NULL, micro_resume)
434 static struct platform_driver micro_device_driver = {
435 .driver = {
436 .name = "ipaq-h3xxx-micro",
437 .pm = &micro_dev_pm_ops,
438 .suppress_bind_attrs = true,
441 builtin_platform_driver_probe(micro_device_driver, micro_probe);