Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/josef/btrfs...
[linux-2.6.git] / drivers / mfd / tps65912-spi.c
blob69a5178bf152a01aca63059778749715f65ed12f
1 /*
2 * tps65912-spi.c -- SPI access for TI TPS65912x PMIC
4 * Copyright 2011 Texas Instruments Inc.
6 * Author: Margarita Olaya Cabrera <magi@slimlogic.co.uk>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * This driver is based on wm8350 implementation.
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/gpio.h>
21 #include <linux/spi/spi.h>
22 #include <linux/mfd/core.h>
23 #include <linux/mfd/tps65912.h>
25 static int tps65912_spi_write(struct tps65912 *tps65912, u8 addr,
26 int bytes, void *src)
28 struct spi_device *spi = tps65912->control_data;
29 u8 *data = (u8 *) src;
30 int ret;
31 /* bit 23 is the read/write bit */
32 unsigned long spi_data = 1 << 23 | addr << 15 | *data;
33 struct spi_transfer xfer;
34 struct spi_message msg;
35 u32 tx_buf, rx_buf;
37 tx_buf = spi_data;
38 rx_buf = 0;
40 xfer.tx_buf = &tx_buf;
41 xfer.rx_buf = NULL;
42 xfer.len = sizeof(unsigned long);
43 xfer.bits_per_word = 24;
45 spi_message_init(&msg);
46 spi_message_add_tail(&xfer, &msg);
48 ret = spi_sync(spi, &msg);
49 return ret;
52 static int tps65912_spi_read(struct tps65912 *tps65912, u8 addr,
53 int bytes, void *dest)
55 struct spi_device *spi = tps65912->control_data;
56 /* bit 23 is the read/write bit */
57 unsigned long spi_data = 0 << 23 | addr << 15;
58 struct spi_transfer xfer;
59 struct spi_message msg;
60 int ret;
61 u8 *data = (u8 *) dest;
62 u32 tx_buf, rx_buf;
64 tx_buf = spi_data;
65 rx_buf = 0;
67 xfer.tx_buf = &tx_buf;
68 xfer.rx_buf = &rx_buf;
69 xfer.len = sizeof(unsigned long);
70 xfer.bits_per_word = 24;
72 spi_message_init(&msg);
73 spi_message_add_tail(&xfer, &msg);
75 if (spi == NULL)
76 return 0;
78 ret = spi_sync(spi, &msg);
79 if (ret == 0)
80 *data = (u8) (rx_buf & 0xFF);
81 return ret;
84 static int tps65912_spi_probe(struct spi_device *spi)
86 struct tps65912 *tps65912;
88 tps65912 = devm_kzalloc(&spi->dev,
89 sizeof(struct tps65912), GFP_KERNEL);
90 if (tps65912 == NULL)
91 return -ENOMEM;
93 tps65912->dev = &spi->dev;
94 tps65912->control_data = spi;
95 tps65912->read = tps65912_spi_read;
96 tps65912->write = tps65912_spi_write;
98 spi_set_drvdata(spi, tps65912);
100 return tps65912_device_init(tps65912);
103 static int tps65912_spi_remove(struct spi_device *spi)
105 struct tps65912 *tps65912 = spi_get_drvdata(spi);
107 tps65912_device_exit(tps65912);
109 return 0;
112 static struct spi_driver tps65912_spi_driver = {
113 .driver = {
114 .name = "tps65912",
115 .owner = THIS_MODULE,
117 .probe = tps65912_spi_probe,
118 .remove = tps65912_spi_remove,
121 static int __init tps65912_spi_init(void)
123 int ret;
125 ret = spi_register_driver(&tps65912_spi_driver);
126 if (ret != 0)
127 pr_err("Failed to register TPS65912 SPI driver: %d\n", ret);
129 return 0;
131 /* init early so consumer devices can complete system boot */
132 subsys_initcall(tps65912_spi_init);
134 static void __exit tps65912_spi_exit(void)
136 spi_unregister_driver(&tps65912_spi_driver);
138 module_exit(tps65912_spi_exit);
140 MODULE_AUTHOR("Margarita Olaya <magi@slimlogic.co.uk>");
141 MODULE_DESCRIPTION("SPI support for TPS65912 chip family mfd");
142 MODULE_LICENSE("GPL");