1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/err.h>
25#include <linux/slab.h>
26#include <linux/i2c.h>
27#include <linux/i2c/pmbus.h>
28#include "pmbus.h"
29
30#define UCD9200_PHASE_INFO 0xd2
31#define UCD9200_DEVICE_ID 0xfd
32
33enum chips { ucd9200, ucd9220, ucd9222, ucd9224, ucd9240, ucd9244, ucd9246,
34 ucd9248 };
35
36static const struct i2c_device_id ucd9200_id[] = {
37 {"ucd9200", ucd9200},
38 {"ucd9220", ucd9220},
39 {"ucd9222", ucd9222},
40 {"ucd9224", ucd9224},
41 {"ucd9240", ucd9240},
42 {"ucd9244", ucd9244},
43 {"ucd9246", ucd9246},
44 {"ucd9248", ucd9248},
45 {}
46};
47MODULE_DEVICE_TABLE(i2c, ucd9200_id);
48
49static int ucd9200_probe(struct i2c_client *client,
50 const struct i2c_device_id *id)
51{
52 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
53 struct pmbus_driver_info *info;
54 const struct i2c_device_id *mid;
55 int i, j, ret;
56
57 if (!i2c_check_functionality(client->adapter,
58 I2C_FUNC_SMBUS_BYTE_DATA |
59 I2C_FUNC_SMBUS_BLOCK_DATA))
60 return -ENODEV;
61
62 ret = i2c_smbus_read_block_data(client, UCD9200_DEVICE_ID,
63 block_buffer);
64 if (ret < 0) {
65 dev_err(&client->dev, "Failed to read device ID\n");
66 return ret;
67 }
68 block_buffer[ret] = '\0';
69 dev_info(&client->dev, "Device ID %s\n", block_buffer);
70
71 for (mid = ucd9200_id; mid->name[0]; mid++) {
72 if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
73 break;
74 }
75 if (!mid->name[0]) {
76 dev_err(&client->dev, "Unsupported device\n");
77 return -ENODEV;
78 }
79 if (id->driver_data != ucd9200 && id->driver_data != mid->driver_data)
80 dev_notice(&client->dev,
81 "Device mismatch: Configured %s, detected %s\n",
82 id->name, mid->name);
83
84 info = devm_kzalloc(&client->dev, sizeof(struct pmbus_driver_info),
85 GFP_KERNEL);
86 if (!info)
87 return -ENOMEM;
88
89 ret = i2c_smbus_read_block_data(client, UCD9200_PHASE_INFO,
90 block_buffer);
91 if (ret < 0) {
92 dev_err(&client->dev, "Failed to read phase information\n");
93 return ret;
94 }
95
96
97
98
99
100
101
102 info->pages = 0;
103 for (i = 0; i < ret; i++) {
104 if (!block_buffer[i])
105 break;
106 info->pages++;
107 }
108 if (!info->pages) {
109 dev_err(&client->dev, "No rails configured\n");
110 return -ENODEV;
111 }
112 dev_info(&client->dev, "%d rails configured\n", info->pages);
113
114
115
116
117
118
119
120
121
122 for (i = 0; i < info->pages; i++) {
123
124
125
126
127 for (j = 0; j < 3; j++) {
128 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
129 if (ret < 0)
130 continue;
131 ret = i2c_smbus_write_byte_data(client, PMBUS_PHASE,
132 0xff);
133 if (ret < 0)
134 continue;
135 break;
136 }
137 if (ret < 0) {
138 dev_err(&client->dev,
139 "Failed to initialize PHASE registers\n");
140 return ret;
141 }
142 }
143 if (info->pages > 1)
144 i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
145
146 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT |
147 PMBUS_HAVE_IIN | PMBUS_HAVE_PIN |
148 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
149 PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
150 PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP |
151 PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
152
153 for (i = 1; i < info->pages; i++)
154 info->func[i] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
155 PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
156 PMBUS_HAVE_POUT |
157 PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
158
159
160 if (mid->driver_data == ucd9240)
161 info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12;
162
163 return pmbus_do_probe(client, mid, info);
164}
165
166
167static struct i2c_driver ucd9200_driver = {
168 .driver = {
169 .name = "ucd9200",
170 },
171 .probe = ucd9200_probe,
172 .remove = pmbus_do_remove,
173 .id_table = ucd9200_id,
174};
175
176module_i2c_driver(ucd9200_driver);
177
178MODULE_AUTHOR("Guenter Roeck");
179MODULE_DESCRIPTION("PMBus driver for TI UCD922x, UCD924x");
180MODULE_LICENSE("GPL");
181