Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * tps6507x.c -- TPS6507x chip family multi-function driver |
| 3 | * |
| 4 | * Copyright (c) 2010 RidgeRun (todd.fischer@ridgerun.com) |
| 5 | * |
| 6 | * Author: Todd Fischer |
| 7 | * todd.fischer@ridgerun.com |
| 8 | * |
| 9 | * Credits: |
| 10 | * |
| 11 | * Using code from wm831x-*.c, wm8400-core, Wolfson Microelectronics PLC. |
| 12 | * |
| 13 | * For licencing details see kernel-base/COPYING |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/moduleparam.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/mfd/core.h> |
| 23 | #include <linux/mfd/tps6507x.h> |
| 24 | |
| 25 | static struct mfd_cell tps6507x_devs[] = { |
| 26 | { |
| 27 | .name = "tps6507x-pmic", |
| 28 | }, |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 29 | { |
| 30 | .name = "tps6507x-ts", |
| 31 | }, |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | |
| 35 | static int tps6507x_i2c_read_device(struct tps6507x_dev *tps6507x, char reg, |
| 36 | int bytes, void *dest) |
| 37 | { |
| 38 | struct i2c_client *i2c = tps6507x->i2c_client; |
| 39 | struct i2c_msg xfer[2]; |
| 40 | int ret; |
| 41 | |
| 42 | /* Write register */ |
| 43 | xfer[0].addr = i2c->addr; |
| 44 | xfer[0].flags = 0; |
| 45 | xfer[0].len = 1; |
| 46 | xfer[0].buf = ® |
| 47 | |
| 48 | /* Read data */ |
| 49 | xfer[1].addr = i2c->addr; |
| 50 | xfer[1].flags = I2C_M_RD; |
| 51 | xfer[1].len = bytes; |
| 52 | xfer[1].buf = dest; |
| 53 | |
| 54 | ret = i2c_transfer(i2c->adapter, xfer, 2); |
| 55 | if (ret == 2) |
| 56 | ret = 0; |
| 57 | else if (ret >= 0) |
| 58 | ret = -EIO; |
| 59 | |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | static int tps6507x_i2c_write_device(struct tps6507x_dev *tps6507x, char reg, |
| 64 | int bytes, void *src) |
| 65 | { |
| 66 | struct i2c_client *i2c = tps6507x->i2c_client; |
| 67 | /* we add 1 byte for device register */ |
| 68 | u8 msg[TPS6507X_MAX_REGISTER + 1]; |
| 69 | int ret; |
| 70 | |
Axel Lin | a8d6aa0 | 2010-10-13 10:44:39 +0800 | [diff] [blame] | 71 | if (bytes > TPS6507X_MAX_REGISTER) |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 72 | return -EINVAL; |
| 73 | |
| 74 | msg[0] = reg; |
| 75 | memcpy(&msg[1], src, bytes); |
| 76 | |
| 77 | ret = i2c_master_send(i2c, msg, bytes + 1); |
| 78 | if (ret < 0) |
| 79 | return ret; |
| 80 | if (ret != bytes + 1) |
| 81 | return -EIO; |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int tps6507x_i2c_probe(struct i2c_client *i2c, |
| 86 | const struct i2c_device_id *id) |
| 87 | { |
| 88 | struct tps6507x_dev *tps6507x; |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 89 | |
Axel Lin | 1881b68 | 2012-12-09 20:25:55 +0800 | [diff] [blame^] | 90 | tps6507x = devm_kzalloc(&i2c->dev, sizeof(struct tps6507x_dev), |
| 91 | GFP_KERNEL); |
Axel Lin | 04a0642 | 2010-08-09 14:49:46 +0800 | [diff] [blame] | 92 | if (tps6507x == NULL) |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 93 | return -ENOMEM; |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 94 | |
| 95 | i2c_set_clientdata(i2c, tps6507x); |
| 96 | tps6507x->dev = &i2c->dev; |
| 97 | tps6507x->i2c_client = i2c; |
| 98 | tps6507x->read_dev = tps6507x_i2c_read_device; |
| 99 | tps6507x->write_dev = tps6507x_i2c_write_device; |
| 100 | |
Axel Lin | 1881b68 | 2012-12-09 20:25:55 +0800 | [diff] [blame^] | 101 | return mfd_add_devices(tps6507x->dev, -1, tps6507x_devs, |
| 102 | ARRAY_SIZE(tps6507x_devs), NULL, 0, NULL); |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static int tps6507x_i2c_remove(struct i2c_client *i2c) |
| 106 | { |
| 107 | struct tps6507x_dev *tps6507x = i2c_get_clientdata(i2c); |
| 108 | |
| 109 | mfd_remove_devices(tps6507x->dev); |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static const struct i2c_device_id tps6507x_i2c_id[] = { |
| 114 | { "tps6507x", 0 }, |
| 115 | { } |
| 116 | }; |
| 117 | MODULE_DEVICE_TABLE(i2c, tps6507x_i2c_id); |
| 118 | |
| 119 | |
| 120 | static struct i2c_driver tps6507x_i2c_driver = { |
| 121 | .driver = { |
| 122 | .name = "tps6507x", |
| 123 | .owner = THIS_MODULE, |
| 124 | }, |
| 125 | .probe = tps6507x_i2c_probe, |
| 126 | .remove = tps6507x_i2c_remove, |
| 127 | .id_table = tps6507x_i2c_id, |
| 128 | }; |
| 129 | |
| 130 | static int __init tps6507x_i2c_init(void) |
| 131 | { |
| 132 | return i2c_add_driver(&tps6507x_i2c_driver); |
| 133 | } |
| 134 | /* init early so consumer devices can complete system boot */ |
| 135 | subsys_initcall(tps6507x_i2c_init); |
| 136 | |
| 137 | static void __exit tps6507x_i2c_exit(void) |
| 138 | { |
| 139 | i2c_del_driver(&tps6507x_i2c_driver); |
| 140 | } |
| 141 | module_exit(tps6507x_i2c_exit); |
| 142 | |
| 143 | MODULE_DESCRIPTION("TPS6507x chip family multi-function driver"); |
| 144 | MODULE_LICENSE("GPL"); |