proc: use seq_puts()/seq_putc() where possible
[linux-2.6/next.git] / drivers / mfd / ab8500-spi.c
blobb1653421edb5480005f03dc6d4e3afa8b337b963
1 /*
2 * Copyright (C) ST-Ericsson SA 2010
4 * License Terms: GNU General Public License v2
5 * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
6 */
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/spi/spi.h>
14 #include <linux/mfd/ab8500.h>
17 * This funtion writes to any AB8500 registers using
18 * SPI protocol & before it writes it packs the data
19 * in the below 24 bit frame format
21 * *|------------------------------------|
22 * *| 23|22...18|17.......10|9|8|7......0|
23 * *| r/w bank adr data |
24 * * ------------------------------------
26 * This function shouldn't be called from interrupt
27 * context
29 static int ab8500_spi_write(struct ab8500 *ab8500, u16 addr, u8 data)
31 struct spi_device *spi = container_of(ab8500->dev, struct spi_device,
32 dev);
33 unsigned long spi_data = addr << 10 | data;
34 struct spi_transfer xfer;
35 struct spi_message msg;
37 ab8500->tx_buf[0] = spi_data;
38 ab8500->rx_buf[0] = 0;
40 xfer.tx_buf = ab8500->tx_buf;
41 xfer.rx_buf = NULL;
42 xfer.len = sizeof(unsigned long);
44 spi_message_init(&msg);
45 spi_message_add_tail(&xfer, &msg);
47 return spi_sync(spi, &msg);
50 static int ab8500_spi_read(struct ab8500 *ab8500, u16 addr)
52 struct spi_device *spi = container_of(ab8500->dev, struct spi_device,
53 dev);
54 unsigned long spi_data = 1 << 23 | addr << 10;
55 struct spi_transfer xfer;
56 struct spi_message msg;
57 int ret;
59 ab8500->tx_buf[0] = spi_data;
60 ab8500->rx_buf[0] = 0;
62 xfer.tx_buf = ab8500->tx_buf;
63 xfer.rx_buf = ab8500->rx_buf;
64 xfer.len = sizeof(unsigned long);
66 spi_message_init(&msg);
67 spi_message_add_tail(&xfer, &msg);
69 ret = spi_sync(spi, &msg);
70 if (!ret)
72 * Only the 8 lowermost bytes are
73 * defined with value, the rest may
74 * vary depending on chip/board noise.
76 ret = ab8500->rx_buf[0] & 0xFFU;
78 return ret;
81 static int __devinit ab8500_spi_probe(struct spi_device *spi)
83 struct ab8500 *ab8500;
84 int ret;
86 spi->bits_per_word = 24;
87 ret = spi_setup(spi);
88 if (ret < 0)
89 return ret;
91 ab8500 = kzalloc(sizeof *ab8500, GFP_KERNEL);
92 if (!ab8500)
93 return -ENOMEM;
95 ab8500->dev = &spi->dev;
96 ab8500->irq = spi->irq;
98 ab8500->read = ab8500_spi_read;
99 ab8500->write = ab8500_spi_write;
101 spi_set_drvdata(spi, ab8500);
103 ret = ab8500_init(ab8500);
104 if (ret)
105 kfree(ab8500);
107 return ret;
110 static int __devexit ab8500_spi_remove(struct spi_device *spi)
112 struct ab8500 *ab8500 = spi_get_drvdata(spi);
114 ab8500_exit(ab8500);
115 kfree(ab8500);
117 return 0;
120 static struct spi_driver ab8500_spi_driver = {
121 .driver = {
122 .name = "ab8500-spi",
123 .owner = THIS_MODULE,
125 .probe = ab8500_spi_probe,
126 .remove = __devexit_p(ab8500_spi_remove)
129 static int __init ab8500_spi_init(void)
131 return spi_register_driver(&ab8500_spi_driver);
133 subsys_initcall(ab8500_spi_init);
135 static void __exit ab8500_spi_exit(void)
137 spi_unregister_driver(&ab8500_spi_driver);
139 module_exit(ab8500_spi_exit);
141 MODULE_AUTHOR("Srinidhi KASAGAR <srinidhi.kasagar@stericsson.com");
142 MODULE_DESCRIPTION("AB8500 SPI");
143 MODULE_LICENSE("GPL v2");