2 * Copyright(c) 2009 Dialog Semiconductor Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * da9052-i2c.c: I2C SSC (Synchronous Serial Communication) driver for DA9052
12 #include <linux/device.h>
13 #include <linux/mfd/core.h>
14 #include <linux/i2c.h>
15 #include <linux/mfd/da9052/da9052.h>
16 #include <linux/mfd/da9052/reg.h>
18 static struct da9052 *da9052_i2c;
20 #define I2C_CONNECTED 0
22 static int da9052_i2c_is_connected(void)
24 struct da9052_ssc_msg msg;
25 int retries = 10, ret = -1;
27 msg.addr = DA9052_INTERFACE_REG;
29 /* Test i2c connectivity by reading the GPIO_0-1 register */
30 if (0 != da9052_i2c_read(da9052_i2c, &msg)) {
31 printk(KERN_INFO"da9052_i2c_is_connected - i2c read failed.....\n");
33 printk(KERN_INFO"da9052_i2c_is_connected - i2c read success....\n");
36 } while (ret != 0 && retries--);
41 static int __devinit da9052_i2c_probe(struct i2c_client *client,
42 const struct i2c_device_id *id)
44 struct i2c_adapter *adapter;
45 // printk("\n\tEntered da9052_i2c_is_probe.............\n");
47 da9052_i2c = kzalloc(sizeof(struct da9052), GFP_KERNEL);
52 /* Get the bus driver handler */
53 adapter = to_i2c_adapter(client->dev.parent);
55 /* Check i2c bus driver supports byte data transfer */
56 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
57 dev_info(&client->dev,\
58 "Error in %s:i2c_check_functionality\n", __func__);
62 /* Store handle to i2c client */
63 da9052_i2c->i2c_client = client;
64 da9052_i2c->irq = client->irq;
66 da9052_i2c->dev = &client->dev;
68 /* Initialize i2c data structure here*/
69 da9052_i2c->adapter = adapter;
71 /* host i2c driver looks only first 7 bits for the slave address */
72 da9052_i2c->slave_addr = DA9052_I2C_ADDR >> 1;
74 /* Store the i2c client data */
75 i2c_set_clientdata(client, da9052_i2c);
77 /* Validate I2C connectivity */
78 if ( I2C_CONNECTED == da9052_i2c_is_connected()) {
79 /* I2C is connected */
80 da9052_i2c->connecting_device = I2C;
81 if( 0!= da9052_ssc_init(da9052_i2c) )
88 //printk("Exiting da9052_i2c_probe.....\n");
93 static int da9052_i2c_remove(struct i2c_client *client)
96 struct da9052 *da9052 = i2c_get_clientdata(client);
98 mfd_remove_devices(da9052->dev);
103 int da9052_i2c_write(struct da9052 *da9052, struct da9052_ssc_msg *msg)
105 struct i2c_msg i2cmsg;
106 unsigned char buf[2] = {0};
109 /* Copy the ssc msg to local character buffer */
113 /*Construct a i2c msg for a da9052 driver ssc message request */
114 i2cmsg.addr = da9052->slave_addr;
118 /* To write the data on I2C set flag to zero */
121 /* Start the i2c transfer by calling host i2c driver function */
122 ret = i2c_transfer(da9052->adapter, &i2cmsg, 1);
125 dev_info(&da9052->i2c_client->dev,\
126 "_%s:master_xfer Failed!!\n", __func__);
133 int da9052_i2c_read(struct da9052 *da9052, struct da9052_ssc_msg *msg)
136 /*Get the da9052_i2c client details*/
137 unsigned char buf[2] = {0, 0};
138 struct i2c_msg i2cmsg[2];
141 /* Copy SSC Msg to local character buffer */
144 /*Construct a i2c msg for a da9052 driver ssc message request */
145 i2cmsg[0].addr = da9052->slave_addr ;
147 i2cmsg[0].buf = &buf[0];
149 /*To write the data on I2C set flag to zero */
152 /* Read the data from da9052*/
153 /*Construct a i2c msg for a da9052 driver ssc message request */
154 i2cmsg[1].addr = da9052->slave_addr ;
156 i2cmsg[1].buf = &buf[1];
158 /*To read the data on I2C set flag to I2C_M_RD */
159 i2cmsg[1].flags = I2C_M_RD;
161 /* Start the i2c transfer by calling host i2c driver function */
162 ret = i2c_transfer(da9052->adapter, i2cmsg, 2);
164 dev_info(&da9052->i2c_client->dev,\
165 "2 - %s:master_xfer Failed!!\n", __func__);
169 msg->data = *i2cmsg[1].buf;
174 int da9052_i2c_write_many(struct da9052 *da9052,
175 struct da9052_ssc_msg *sscmsg, int msg_no)
178 struct i2c_msg i2cmsg;
179 unsigned char data_buf[MAX_READ_WRITE_CNT+1];
180 struct da9052_ssc_msg ctrlb_msg;
181 struct da9052_ssc_msg *msg_queue = sscmsg;
183 /* Flag to check if requested registers are contiguous */
184 unsigned char cont_data = 1;
185 unsigned char cnt = 0;
187 /* Check if requested registers are contiguous */
188 for (cnt = 1; cnt < msg_no; cnt++) {
189 if ((msg_queue[cnt].addr - msg_queue[cnt-1].addr) != 1) {
190 /* Difference is not 1, i.e. non-contiguous registers */
196 if (cont_data == 0) {
197 /* Requested registers are non-contiguous */
198 for (cnt = 0; cnt < msg_no; cnt++) {
199 ret = da9052->write(da9052, &msg_queue[cnt]);
206 * Requested registers are contiguous
207 * or PAGE WRITE sequence of I2C transactions is as below
208 * (slave_addr + reg_addr + data_1 + data_2 + ...)
209 * First read current WRITE MODE via CONTROL_B register of DA9052
211 ctrlb_msg.addr = DA9052_CONTROLB_REG;
212 ctrlb_msg.data = 0x0;
213 ret = da9052->read(da9052, &ctrlb_msg);
218 /* Check if PAGE WRITE mode is set */
219 if (ctrlb_msg.data & DA9052_CONTROLB_WRITEMODE) {
220 /* REPEAT WRITE mode is configured */
221 /* Now set DA9052 into PAGE WRITE mode */
222 ctrlb_msg.data &= ~DA9052_CONTROLB_WRITEMODE;
223 ret = da9052->write(da9052, &ctrlb_msg);
229 /* Put first register address */
230 data_buf[0] = msg_queue[0].addr;
232 for (cnt = 0; cnt < msg_no; cnt++)
233 data_buf[cnt+1] = msg_queue[cnt].data;
235 /* Construct a i2c msg for PAGE WRITE */
236 i2cmsg.addr = da9052->slave_addr ;
237 /* First register address + all data*/
238 i2cmsg.len = (msg_no + 1);
239 i2cmsg.buf = data_buf;
241 /*To write the data on I2C set flag to zero */
244 /* Start the i2c transfer by calling host i2c driver function */
245 ret = i2c_transfer(da9052->adapter, &i2cmsg, 1);
247 dev_info(&da9052->i2c_client->dev,\
248 "1 - i2c_transfer function falied in [%s]!!!\n", __func__);
255 int da9052_i2c_read_many(struct da9052 *da9052,
256 struct da9052_ssc_msg *sscmsg, int msg_no)
259 struct i2c_msg i2cmsg;
260 unsigned char data_buf[MAX_READ_WRITE_CNT];
261 struct da9052_ssc_msg *msg_queue = sscmsg;
263 /* Flag to check if requested registers are contiguous */
264 unsigned char cont_data = 1;
265 unsigned char cnt = 0;
267 /* Check if requested registers are contiguous */
268 for (cnt = 1; cnt < msg_no; cnt++) {
269 if ((msg_queue[cnt].addr - msg_queue[cnt-1].addr) != 1) {
270 /* Difference is not 1, i.e. non-contiguous registers */
276 if (cont_data == 0) {
277 /* Requested registers are non-contiguous */
278 for (cnt = 0; cnt < msg_no; cnt++) {
279 ret = da9052->read(da9052, &msg_queue[cnt]);
281 dev_info(&da9052->i2c_client->dev,\
282 "Error in %s", __func__);
290 * We want to perform PAGE READ via I2C
291 * For PAGE READ sequence of I2C transactions is as below
292 * (slave_addr + reg_addr) + (slave_addr + data_1 + data_2 + ...)
294 /* Copy address of first register */
295 data_buf[0] = msg_queue[0].addr;
297 /* Construct a i2c msg for first transaction of PAGE READ i.e. write */
298 i2cmsg.addr = da9052->slave_addr ;
300 i2cmsg.buf = data_buf;
302 /*To write the data on I2C set flag to zero */
305 /* Start the i2c transfer by calling host i2c driver function */
306 ret = i2c_transfer(da9052->adapter, &i2cmsg, 1);
308 dev_info(&da9052->i2c_client->dev,\
309 "1 - i2c_transfer function falied in [%s]!!!\n", __func__);
313 /* Now Read the data from da9052 */
314 /* Construct a i2c msg for second transaction of PAGE READ i.e. read */
315 i2cmsg.addr = da9052->slave_addr ;
317 i2cmsg.buf = data_buf;
319 /*To read the data on I2C set flag to I2C_M_RD */
320 i2cmsg.flags = I2C_M_RD;
322 /* Start the i2c transfer by calling host i2c driver function */
323 ret = i2c_transfer(da9052->adapter,
326 dev_info(&da9052->i2c_client->dev,\
327 "2 - i2c_transfer function falied in [%s]!!!\n", __func__);
331 /* Gather READ data */
332 for (cnt = 0; cnt < msg_no; cnt++)
333 sscmsg[cnt].data = data_buf[cnt];
338 static struct i2c_device_id da9052_ssc_id[] = {
339 { DA9052_SSC_I2C_DEVICE_NAME, 0},
343 static struct i2c_driver da9052_i2c_driver = {
345 .name = DA9052_SSC_I2C_DEVICE_NAME,
346 .owner = THIS_MODULE,
348 .probe = da9052_i2c_probe,
349 .remove = da9052_i2c_remove,
350 .id_table = da9052_ssc_id,
353 static int __init da9052_i2c_init(void)
356 // printk("\n\nEntered da9052_i2c_init................\n\n");
357 ret = i2c_add_driver(&da9052_i2c_driver);
359 printk(KERN_ERR "Unable to register %s\n", DA9052_SSC_I2C_DEVICE_NAME);
364 subsys_initcall(da9052_i2c_init);
366 static void __exit da9052_i2c_exit(void)
368 i2c_del_driver(&da9052_i2c_driver);
370 module_exit(da9052_i2c_exit);
372 MODULE_AUTHOR("Dialog Semiconductor Ltd <dchen@diasemi.com>");
373 MODULE_DESCRIPTION("I2C driver for Dialog DA9052 PMIC");
374 MODULE_LICENSE("GPL v2");
375 MODULE_ALIAS("platform:" DA9052_SSC_I2C_DEVICE_NAME);