summaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/pmbus/pim4328.c
blob: 31d9ae06379a1ae000342c5686e852d4131e5861 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Hardware monitoring driver for PIM4006, PIM4328 and PIM4820
 *
 * Copyright (c) 2021 Flextronics International Sweden AB
 */

#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pmbus.h>
#include <linux/slab.h>
#include "pmbus.h"

enum chips { pim4006, pim4328, pim4820 };

struct pim4328_data {
	enum chips id;
	struct pmbus_driver_info info;
};

#define to_pim4328_data(x)  container_of(x, struct pim4328_data, info)

/* PIM4006 and PIM4328 */
#define PIM4328_MFR_READ_VINA		0xd3
#define PIM4328_MFR_READ_VINB		0xd4

/* PIM4006 */
#define PIM4328_MFR_READ_IINA		0xd6
#define PIM4328_MFR_READ_IINB		0xd7
#define PIM4328_MFR_FET_CHECKSTATUS	0xd9

/* PIM4328 */
#define PIM4328_MFR_STATUS_BITS		0xd5

/* PIM4820 */
#define PIM4328_MFR_READ_STATUS		0xd0

static const struct i2c_device_id pim4328_id[] = {
	{"bmr455", pim4328},
	{"pim4006", pim4006},
	{"pim4106", pim4006},
	{"pim4206", pim4006},
	{"pim4306", pim4006},
	{"pim4328", pim4328},
	{"pim4406", pim4006},
	{"pim4820", pim4820},
	{}
};
MODULE_DEVICE_TABLE(i2c, pim4328_id);

static int pim4328_read_word_data(struct i2c_client *client, int page,
				  int phase, int reg)
{
	int ret;

	if (page > 0)
		return -ENXIO;

	if (phase == 0xff)
		return -ENODATA;

	switch (reg) {
	case PMBUS_READ_VIN:
		ret = pmbus_read_word_data(client, page, phase,
					   phase == 0 ? PIM4328_MFR_READ_VINA
						      : PIM4328_MFR_READ_VINB);
		break;
	case PMBUS_READ_IIN:
		ret = pmbus_read_word_data(client, page, phase,
					   phase == 0 ? PIM4328_MFR_READ_IINA
						      : PIM4328_MFR_READ_IINB);
		break;
	default:
		ret = -ENODATA;
	}

	return ret;
}

static int pim4328_read_byte_data(struct i2c_client *client, int page, int reg)
{
	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
	struct pim4328_data *data = to_pim4328_data(info);
	int ret, status;

	if (page > 0)
		return -ENXIO;

	switch (reg) {
	case PMBUS_STATUS_BYTE:
		ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE);
		if (ret < 0)
			return ret;
		if (data->id == pim4006) {
			status = pmbus_read_word_data(client, page, 0xff,
						      PIM4328_MFR_FET_CHECKSTATUS);
			if (status < 0)
				return status;
			if (status & 0x0630) /* Input UV */
				ret |= PB_STATUS_VIN_UV;
		} else if (data->id == pim4328) {
			status = pmbus_read_byte_data(client, page,
						      PIM4328_MFR_STATUS_BITS);
			if (status < 0)
				return status;
			if (status & 0x04) /* Input UV */
				ret |= PB_STATUS_VIN_UV;
			if (status & 0x40) /* Output UV */
				ret |= PB_STATUS_NONE_ABOVE;
		} else if (data->id == pim4820) {
			status = pmbus_read_byte_data(client, page,
						      PIM4328_MFR_READ_STATUS);
			if (status < 0)
				return status;
			if (status & 0x05) /* Input OV or OC */
				ret |= PB_STATUS_NONE_ABOVE;
			if (status & 0x1a) /* Input UV */
				ret |= PB_STATUS_VIN_UV;
			if (status & 0x40) /* OT */
				ret |= PB_STATUS_TEMPERATURE;
		}
		break;
	default:
		ret = -ENODATA;
	}

	return ret;
}

static int pim4328_probe(struct i2c_client *client)
{
	int status;
	u8 device_id[I2C_SMBUS_BLOCK_MAX + 1];
	const struct i2c_device_id *mid;
	struct pim4328_data *data;
	struct pmbus_driver_info *info;
	struct pmbus_platform_data *pdata;
	struct device *dev = &client->dev;

	if (!i2c_check_functionality(client->adapter,
				     I2C_FUNC_SMBUS_READ_BYTE_DATA
				     | I2C_FUNC_SMBUS_BLOCK_DATA))
		return -ENODEV;

	data = devm_kzalloc(&client->dev, sizeof(struct pim4328_data),
			    GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	status = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, device_id);
	if (status < 0) {
		dev_err(&client->dev, "Failed to read Manufacturer Model\n");
		return status;
	}
	for (mid = pim4328_id; mid->name[0]; mid++) {
		if (!strncasecmp(mid->name, device_id, strlen(mid->name)))
			break;
	}
	if (!mid->name[0]) {
		dev_err(&client->dev, "Unsupported device\n");
		return -ENODEV;
	}

	if (strcmp(client->name, mid->name))
		dev_notice(&client->dev,
			   "Device mismatch: Configured %s, detected %s\n",
			   client->name, mid->name);

	data->id = mid->driver_data;
	info = &data->info;
	info->pages = 1;
	info->read_byte_data = pim4328_read_byte_data;
	info->read_word_data = pim4328_read_word_data;

	pdata = devm_kzalloc(dev, sizeof(struct pmbus_platform_data),
			     GFP_KERNEL);
	if (!pdata)
		return -ENOMEM;
	dev->platform_data = pdata;
	pdata->flags = PMBUS_NO_CAPABILITY | PMBUS_NO_WRITE_PROTECT;

	switch (data->id) {
	case pim4006:
		info->phases[0] = 2;
		info->func[0] = PMBUS_PHASE_VIRTUAL | PMBUS_HAVE_VIN
			| PMBUS_HAVE_TEMP | PMBUS_HAVE_IOUT;
		info->pfunc[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN;
		info->pfunc[1] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN;
		break;
	case pim4328:
		info->phases[0] = 2;
		info->func[0] = PMBUS_PHASE_VIRTUAL
			| PMBUS_HAVE_VCAP | PMBUS_HAVE_VIN
			| PMBUS_HAVE_TEMP | PMBUS_HAVE_IOUT;
		info->pfunc[0] = PMBUS_HAVE_VIN;
		info->pfunc[1] = PMBUS_HAVE_VIN;
		info->format[PSC_VOLTAGE_IN] = direct;
		info->format[PSC_TEMPERATURE] = direct;
		info->format[PSC_CURRENT_OUT] = direct;
		pdata->flags |= PMBUS_USE_COEFFICIENTS_CMD;
		break;
	case pim4820:
		info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_TEMP
			| PMBUS_HAVE_IIN;
		info->format[PSC_VOLTAGE_IN] = direct;
		info->format[PSC_TEMPERATURE] = direct;
		info->format[PSC_CURRENT_IN] = direct;
		pdata->flags |= PMBUS_USE_COEFFICIENTS_CMD;
		break;
	default:
		return -ENODEV;
	}

	return pmbus_do_probe(client, info);
}

static struct i2c_driver pim4328_driver = {
	.driver = {
		   .name = "pim4328",
		   },
	.probe = pim4328_probe,
	.id_table = pim4328_id,
};

module_i2c_driver(pim4328_driver);

MODULE_AUTHOR("Erik Rosen <erik.rosen@metormote.com>");
MODULE_DESCRIPTION("PMBus driver for PIM4006, PIM4328, PIM4820 power interface modules");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS(PMBUS);