Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * tps6507x.c  --  TPS6507x chip family multi-function driver
0003  *
0004  *  Copyright (c) 2010 RidgeRun (todd.fischer@ridgerun.com)
0005  *
0006  * Author: Todd Fischer
0007  *         todd.fischer@ridgerun.com
0008  *
0009  * Credits:
0010  *
0011  *    Using code from wm831x-*.c, wm8400-core, Wolfson Microelectronics PLC.
0012  *
0013  * For licencing details see kernel-base/COPYING
0014  *
0015  */
0016 
0017 #include <linux/module.h>
0018 #include <linux/moduleparam.h>
0019 #include <linux/init.h>
0020 #include <linux/slab.h>
0021 #include <linux/i2c.h>
0022 #include <linux/of.h>
0023 #include <linux/of_device.h>
0024 #include <linux/mfd/core.h>
0025 #include <linux/mfd/tps6507x.h>
0026 
0027 static const struct mfd_cell tps6507x_devs[] = {
0028     {
0029         .name = "tps6507x-pmic",
0030     },
0031     {
0032         .name = "tps6507x-ts",
0033     },
0034 };
0035 
0036 
0037 static int tps6507x_i2c_read_device(struct tps6507x_dev *tps6507x, char reg,
0038                   int bytes, void *dest)
0039 {
0040     struct i2c_client *i2c = tps6507x->i2c_client;
0041     struct i2c_msg xfer[2];
0042     int ret;
0043 
0044     /* Write register */
0045     xfer[0].addr = i2c->addr;
0046     xfer[0].flags = 0;
0047     xfer[0].len = 1;
0048     xfer[0].buf = &reg;
0049 
0050     /* Read data */
0051     xfer[1].addr = i2c->addr;
0052     xfer[1].flags = I2C_M_RD;
0053     xfer[1].len = bytes;
0054     xfer[1].buf = dest;
0055 
0056     ret = i2c_transfer(i2c->adapter, xfer, 2);
0057     if (ret == 2)
0058         ret = 0;
0059     else if (ret >= 0)
0060         ret = -EIO;
0061 
0062     return ret;
0063 }
0064 
0065 static int tps6507x_i2c_write_device(struct tps6507x_dev *tps6507x, char reg,
0066                    int bytes, void *src)
0067 {
0068     struct i2c_client *i2c = tps6507x->i2c_client;
0069     /* we add 1 byte for device register */
0070     u8 msg[TPS6507X_MAX_REGISTER + 1];
0071     int ret;
0072 
0073     if (bytes > TPS6507X_MAX_REGISTER)
0074         return -EINVAL;
0075 
0076     msg[0] = reg;
0077     memcpy(&msg[1], src, bytes);
0078 
0079     ret = i2c_master_send(i2c, msg, bytes + 1);
0080     if (ret < 0)
0081         return ret;
0082     if (ret != bytes + 1)
0083         return -EIO;
0084     return 0;
0085 }
0086 
0087 static int tps6507x_i2c_probe(struct i2c_client *i2c,
0088                 const struct i2c_device_id *id)
0089 {
0090     struct tps6507x_dev *tps6507x;
0091 
0092     tps6507x = devm_kzalloc(&i2c->dev, sizeof(struct tps6507x_dev),
0093                 GFP_KERNEL);
0094     if (tps6507x == NULL)
0095         return -ENOMEM;
0096 
0097     i2c_set_clientdata(i2c, tps6507x);
0098     tps6507x->dev = &i2c->dev;
0099     tps6507x->i2c_client = i2c;
0100     tps6507x->read_dev = tps6507x_i2c_read_device;
0101     tps6507x->write_dev = tps6507x_i2c_write_device;
0102 
0103     return devm_mfd_add_devices(tps6507x->dev, -1, tps6507x_devs,
0104                     ARRAY_SIZE(tps6507x_devs), NULL, 0, NULL);
0105 }
0106 
0107 static const struct i2c_device_id tps6507x_i2c_id[] = {
0108     { "tps6507x", 0 },
0109     { }
0110 };
0111 MODULE_DEVICE_TABLE(i2c, tps6507x_i2c_id);
0112 
0113 #ifdef CONFIG_OF
0114 static const struct of_device_id tps6507x_of_match[] = {
0115     {.compatible = "ti,tps6507x", },
0116     {},
0117 };
0118 MODULE_DEVICE_TABLE(of, tps6507x_of_match);
0119 #endif
0120 
0121 static struct i2c_driver tps6507x_i2c_driver = {
0122     .driver = {
0123            .name = "tps6507x",
0124            .of_match_table = of_match_ptr(tps6507x_of_match),
0125     },
0126     .probe = tps6507x_i2c_probe,
0127     .id_table = tps6507x_i2c_id,
0128 };
0129 
0130 static int __init tps6507x_i2c_init(void)
0131 {
0132     return i2c_add_driver(&tps6507x_i2c_driver);
0133 }
0134 /* init early so consumer devices can complete system boot */
0135 subsys_initcall(tps6507x_i2c_init);
0136 
0137 static void __exit tps6507x_i2c_exit(void)
0138 {
0139     i2c_del_driver(&tps6507x_i2c_driver);
0140 }
0141 module_exit(tps6507x_i2c_exit);
0142 
0143 MODULE_DESCRIPTION("TPS6507x chip family multi-function driver");
0144 MODULE_LICENSE("GPL");