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> |
Sachin Kamat | 5c8c879 | 2013-10-16 14:26:53 +0530 | [diff] [blame] | 22 | #include <linux/of.h> |
Vishwanathrao Badarkhe, Manish | 154c4c7 | 2013-01-24 16:25:17 +0530 | [diff] [blame] | 23 | #include <linux/of_device.h> |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 24 | #include <linux/mfd/core.h> |
| 25 | #include <linux/mfd/tps6507x.h> |
| 26 | |
Geert Uytterhoeven | 30fe2b5 | 2013-11-18 14:33:02 +0100 | [diff] [blame] | 27 | static const struct mfd_cell tps6507x_devs[] = { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 28 | { |
| 29 | .name = "tps6507x-pmic", |
| 30 | }, |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 31 | { |
| 32 | .name = "tps6507x-ts", |
| 33 | }, |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | |
| 37 | static int tps6507x_i2c_read_device(struct tps6507x_dev *tps6507x, char reg, |
| 38 | int bytes, void *dest) |
| 39 | { |
| 40 | struct i2c_client *i2c = tps6507x->i2c_client; |
| 41 | struct i2c_msg xfer[2]; |
| 42 | int ret; |
| 43 | |
| 44 | /* Write register */ |
| 45 | xfer[0].addr = i2c->addr; |
| 46 | xfer[0].flags = 0; |
| 47 | xfer[0].len = 1; |
| 48 | xfer[0].buf = ® |
| 49 | |
| 50 | /* Read data */ |
| 51 | xfer[1].addr = i2c->addr; |
| 52 | xfer[1].flags = I2C_M_RD; |
| 53 | xfer[1].len = bytes; |
| 54 | xfer[1].buf = dest; |
| 55 | |
| 56 | ret = i2c_transfer(i2c->adapter, xfer, 2); |
| 57 | if (ret == 2) |
| 58 | ret = 0; |
| 59 | else if (ret >= 0) |
| 60 | ret = -EIO; |
| 61 | |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | static int tps6507x_i2c_write_device(struct tps6507x_dev *tps6507x, char reg, |
| 66 | int bytes, void *src) |
| 67 | { |
| 68 | struct i2c_client *i2c = tps6507x->i2c_client; |
| 69 | /* we add 1 byte for device register */ |
| 70 | u8 msg[TPS6507X_MAX_REGISTER + 1]; |
| 71 | int ret; |
| 72 | |
Axel Lin | a8d6aa0 | 2010-10-13 10:44:39 +0800 | [diff] [blame] | 73 | if (bytes > TPS6507X_MAX_REGISTER) |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 74 | return -EINVAL; |
| 75 | |
| 76 | msg[0] = reg; |
| 77 | memcpy(&msg[1], src, bytes); |
| 78 | |
| 79 | ret = i2c_master_send(i2c, msg, bytes + 1); |
| 80 | if (ret < 0) |
| 81 | return ret; |
| 82 | if (ret != bytes + 1) |
| 83 | return -EIO; |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int tps6507x_i2c_probe(struct i2c_client *i2c, |
| 88 | const struct i2c_device_id *id) |
| 89 | { |
| 90 | struct tps6507x_dev *tps6507x; |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 91 | |
Axel Lin | 1881b68 | 2012-12-09 20:25:55 +0800 | [diff] [blame] | 92 | tps6507x = devm_kzalloc(&i2c->dev, sizeof(struct tps6507x_dev), |
| 93 | GFP_KERNEL); |
Axel Lin | 04a0642 | 2010-08-09 14:49:46 +0800 | [diff] [blame] | 94 | if (tps6507x == NULL) |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 95 | return -ENOMEM; |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 96 | |
| 97 | i2c_set_clientdata(i2c, tps6507x); |
| 98 | tps6507x->dev = &i2c->dev; |
| 99 | tps6507x->i2c_client = i2c; |
| 100 | tps6507x->read_dev = tps6507x_i2c_read_device; |
| 101 | tps6507x->write_dev = tps6507x_i2c_write_device; |
| 102 | |
Laxman Dewangan | 5635d99 | 2016-04-08 00:13:11 +0530 | [diff] [blame] | 103 | return devm_mfd_add_devices(tps6507x->dev, -1, tps6507x_devs, |
| 104 | ARRAY_SIZE(tps6507x_devs), NULL, 0, NULL); |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static const struct i2c_device_id tps6507x_i2c_id[] = { |
Ben Copeland | 7657c9f | 2016-06-09 16:25:50 +0100 | [diff] [blame] | 108 | { "tps6507x", 0 }, |
| 109 | { } |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 110 | }; |
| 111 | MODULE_DEVICE_TABLE(i2c, tps6507x_i2c_id); |
| 112 | |
Vishwanathrao Badarkhe, Manish | 154c4c7 | 2013-01-24 16:25:17 +0530 | [diff] [blame] | 113 | #ifdef CONFIG_OF |
Jingoo Han | 153a4ff | 2014-05-07 17:22:28 +0900 | [diff] [blame] | 114 | static const struct of_device_id tps6507x_of_match[] = { |
Vishwanathrao Badarkhe, Manish | 154c4c7 | 2013-01-24 16:25:17 +0530 | [diff] [blame] | 115 | {.compatible = "ti,tps6507x", }, |
| 116 | {}, |
| 117 | }; |
| 118 | MODULE_DEVICE_TABLE(of, tps6507x_of_match); |
| 119 | #endif |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 120 | |
| 121 | static struct i2c_driver tps6507x_i2c_driver = { |
| 122 | .driver = { |
| 123 | .name = "tps6507x", |
Vishwanathrao Badarkhe, Manish | 154c4c7 | 2013-01-24 16:25:17 +0530 | [diff] [blame] | 124 | .of_match_table = of_match_ptr(tps6507x_of_match), |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 125 | }, |
| 126 | .probe = tps6507x_i2c_probe, |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 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"); |