summaryrefslogtreecommitdiffstats
path: root/drivers/misc/ad525x_dpot.c
blob: e6b274b5dbd61e6e4bb15b040fc4b500e6916236 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
 * ad525x_dpot: Driver for the Analog Devices AD525x digital potentiometers
 * Copyright (c) 2009 Analog Devices, Inc.
 * Author: Michael Hennerich <hennerich@blackfin.uclinux.org>
 *
 * DEVID		#Wipers		#Positions 	Resistor Options (kOhm)
 * AD5258		1		64		1, 10, 50, 100
 * AD5259		1		256		5, 10, 50, 100
 * AD5251		2		64		1, 10, 50, 100
 * AD5252		2		256		1, 10, 50, 100
 * AD5255		3		512		25, 250
 * AD5253		4		64		1, 10, 50, 100
 * AD5254		4		256		1, 10, 50, 100
 *
 * See Documentation/misc-devices/ad525x_dpot.txt for more info.
 *
 * derived from ad5258.c
 * Copyright (c) 2009 Cyber Switching, Inc.
 * Author: Chris Verges <chrisv@cyberswitching.com>
 *
 * derived from ad5252.c
 * Copyright (c) 2006 Michael Hennerich <hennerich@blackfin.uclinux.org>
 *
 * Licensed under the GPL-2 or later.
 */

#include <linux/module.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/delay.h>

#define DRIVER_NAME			"ad525x_dpot"
#define DRIVER_VERSION			"0.1"

enum dpot_devid {
	AD5258_ID,
	AD5259_ID,
	AD5251_ID,
	AD5252_ID,
	AD5253_ID,
	AD5254_ID,
	AD5255_ID,
};

#define AD5258_MAX_POSITION		64
#define AD5259_MAX_POSITION		256
#define AD5251_MAX_POSITION		64
#define AD5252_MAX_POSITION		256
#define AD5253_MAX_POSITION		64
#define AD5254_MAX_POSITION		256
#define AD5255_MAX_POSITION		512

#define AD525X_RDAC0		0
#define AD525X_RDAC1		1
#define AD525X_RDAC2		2
#define AD525X_RDAC3		3

#define AD525X_REG_TOL		0x18
#define AD525X_TOL_RDAC0	(AD525X_REG_TOL | AD525X_RDAC0)
#define AD525X_TOL_RDAC1	(AD525X_REG_TOL | AD525X_RDAC1)
#define AD525X_TOL_RDAC2	(AD525X_REG_TOL | AD525X_RDAC2)
#define AD525X_TOL_RDAC3	(AD525X_REG_TOL | AD525X_RDAC3)

/* RDAC-to-EEPROM Interface Commands */
#define AD525X_I2C_RDAC		(0x00 << 5)
#define AD525X_I2C_EEPROM	(0x01 << 5)
#define AD525X_I2C_CMD		(0x80)

#define AD525X_DEC_ALL_6DB	(AD525X_I2C_CMD | (0x4 << 3))
#define AD525X_INC_ALL_6DB	(AD525X_I2C_CMD | (0x9 << 3))
#define AD525X_DEC_ALL		(AD525X_I2C_CMD | (0x6 << 3))
#define AD525X_INC_ALL		(AD525X_I2C_CMD | (0xB << 3))

static s32 ad525x_read(struct i2c_client *client, u8 reg);
static s32 ad525x_write(struct i2c_client *client, u8 reg, u8 value);

/*
 * Client data (each client gets its own)
 */

struct dpot_data {
	struct mutex update_lock;
	unsigned rdac_mask;
	unsigned max_pos;
	unsigned devid;
};

/* sysfs functions */

static ssize_t sysfs_show_reg(struct device *dev,
			      struct device_attribute *attr, char *buf, u32 reg)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct dpot_data *data = i2c_get_clientdata(client);
	s32 value;

	mutex_lock(&data->update_lock);
	value = ad525x_read(client, reg);
	mutex_unlock(&data->update_lock);

	if (value < 0)
		return -EINVAL;
	/*
	 * Let someone else deal with converting this ...
	 * the tolerance is a two-byte value where the MSB
	 * is a sign + integer value, and the LSB is a
	 * decimal value.  See page 18 of the AD5258
	 * datasheet (Rev. A) for more details.
	 */

	if (reg & AD525X_REG_TOL)
		return sprintf(buf, "0x%04x\n", value & 0xFFFF);
	else
		return sprintf(buf, "%u\n", value & data->rdac_mask);
}

static ssize_t sysfs_set_reg(struct device *dev,
			     struct device_attribute *attr,
			     const char *buf, size_t count, u32 reg)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct dpot_data *data = i2c_get_clientdata(client);
	unsigned long value;
	int err;

	err = strict_strtoul(buf, 10, &value);
	if (err)
		return err;

	if (value > data->rdac_mask)
		value = data->rdac_mask;

	mutex_lock(&data->update_lock);
	ad525x_write(client, reg, value);
	if (reg & AD525X_I2C_EEPROM)
		msleep(26);	/* Sleep while the EEPROM updates */
	mutex_unlock(&data->update_lock);

	return count;
}

static ssize_t sysfs_do_cmd(struct device *dev,
			    struct device_attribute *attr,
			    const char *buf, size_t count, u32 reg)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct dpot_data *data = i2c_get_clientdata(client);

	mutex_lock(&data->update_lock);
	ad525x_write(client, reg, 0);
	mutex_unlock(&data->update_lock);

	return count;
}

/* ------------------------------------------------------------------------- */

#define DPOT_DEVICE_SHOW(_name, _reg) static ssize_t \
show_##_name(struct device *dev, \
			  struct device_attribute *attr, char *buf) \
{ \
	return sysfs_show_reg(dev, attr, buf, _reg); \
}

#define DPOT_DEVICE_SET(_name, _reg) static ssize_t \
set_##_name(struct device *dev, \
			 struct device_attribute *attr, \
			 const char *buf, size_t count) \
{ \
	return sysfs_set_reg(dev, attr, buf, count, _reg); \
}

#define DPOT_DEVICE_SHOW_SET(name, reg) \
DPOT_DEVICE_SHOW(name, reg) \
DPOT_DEVICE_SET(name, reg) \
static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, show_##name, set_##name);

#define DPOT_DEVICE_SHOW_ONLY(name, reg) \
DPOT_DEVICE_SHOW(name, reg) \
static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, show_##name, NULL);

DPOT_DEVICE_SHOW_SET(rdac0, AD525X_I2C_RDAC | AD525X_RDAC0);
DPOT_DEVICE_SHOW_SET(eeprom0, AD525X_I2C_EEPROM | AD525X_RDAC0);
DPOT_DEVICE_SHOW_ONLY(tolerance0, AD525X_I2C_EEPROM | AD525X_TOL_RDAC0);

DPOT_DEVICE_SHOW_SET(rdac1, AD525X_I2C_RDAC | AD525X_RDAC1);
DPOT_DEVICE_SHOW_SET(eeprom1, AD525X_I2C_EEPROM | AD525X_RDAC1);
DPOT_DEVICE_SHOW_ONLY(tolerance1, AD525X_I2C_EEPROM | AD525X_TOL_RDAC1);

DPOT_DEVICE_SHOW_SET(rdac2, AD525X_I2C_RDAC | AD525X_RDAC2);
DPOT_DEVICE_SHOW_SET(eeprom2, AD525X_I2C_EEPROM | AD525X_RDAC2);
DPOT_DEVICE_SHOW_ONLY(tolerance2, AD525X_I2C_EEPROM | AD525X_TOL_RDAC2);

DPOT_DEVICE_SHOW_SET(rdac3, AD525X_I2C_RDAC | AD525X_RDAC3);
DPOT_DEVICE_SHOW_SET(eeprom3, AD525X_I2C_EEPROM | AD525X_RDAC3);
DPOT_DEVICE_SHOW_ONLY(tolerance3, AD525X_I2C_EEPROM | AD525X_TOL_RDAC3);

static struct attribute *ad525x_attributes_wipers[4][4] = {
	{
		&dev_attr_rdac0.attr,
		&dev_attr_eeprom0.attr,
		&dev_attr_tolerance0.attr,
		NULL
	}, {
		&dev_attr_rdac1.attr,
		&dev_attr_eeprom1.attr,
		&dev_attr_tolerance1.attr,
		NULL
	}, {
		&dev_attr_rdac2.attr,
		&dev_attr_eeprom2.attr,
		&dev_attr_tolerance2.attr,
		NULL
	}, {
		&dev_attr_rdac3.attr,
		&dev_attr_eeprom3.attr,
		&dev_attr_tolerance3.attr,
		NULL
	}
};

static const struct attribute_group ad525x_group_wipers[] = {
	{.attrs = ad525x_attributes_wipers[AD525X_RDAC0]},
	{.attrs = ad525x_attributes_wipers[AD525X_RDAC1]},
	{.attrs = ad525x_attributes_wipers[AD525X_RDAC2]},
	{.attrs = ad525x_attributes_wipers[AD525X_RDAC3]},
};

/* ------------------------------------------------------------------------- */

#define DPOT_DEVICE_DO_CMD(_name, _cmd) static ssize_t \
set_##_name(struct device *dev, \
			 struct device_attribute *attr, \
			 const char *buf, size_t count) \
{ \
	return sysfs_do_cmd(dev, attr, buf, count, _cmd); \
} \
static DEVICE_ATTR(_name, S_IWUSR | S_IRUGO, NULL, set_##_name);

DPOT_DEVICE_DO_CMD(inc_all, AD525X_INC_ALL);
DPOT_DEVICE_DO_CMD(dec_all, AD525X_DEC_ALL);
DPOT_DEVICE_DO_CMD(inc_all_6db, AD525X_INC_ALL_6DB);
DPOT_DEVICE_DO_CMD(dec_all_6db, AD525X_DEC_ALL_6DB);

static struct attribute *ad525x_attributes_commands[] = {
	&dev_attr_inc_all.attr,
	&dev_attr_dec_all.attr,
	&dev_attr_inc_all_6db.attr,
	&dev_attr_dec_all_6db.attr,
	NULL
};

static const struct attribute_group ad525x_group_commands = {
	.attrs = ad525x_attributes_commands,
};

/* ------------------------------------------------------------------------- */

/* i2c device functions */

/**
 * ad525x_read - return the value contained in the specified register
 * on the AD5258 device.
 * @client: value returned from i2c_new_device()
 * @reg: the register to read
 *
 * If the tolerance register is specified, 2 bytes are returned.
 * Otherwise, 1 byte is returned.  A negative value indicates an error
 * occurred while reading the register.
 */
static s32 ad525x_read(struct i2c_client *client, u8 reg)
{
	struct dpot_data *data = i2c_get_clientdata(client);

	if ((reg & AD525X_REG_TOL) || (data->max_pos > 256))
		return i2c_smbus_read_word_data(client, (reg & 0xF8) |
						((reg & 0x7) << 1));
	else
		return i2c_smbus_read_byte_data(client, reg);
}

/**
 * ad525x_write - store the given value in the specified register on
 * the AD5258 device.
 * @client: value returned from i2c_new_device()
 * @reg: the register to write
 * @value: the byte to store in the register
 *
 * For certain instructions that do not require a data byte, "NULL"
 * should be specified for the "value" parameter.  These instructions
 * include NOP, RESTORE_FROM_EEPROM, and STORE_TO_EEPROM.
 *
 * A negative return value indicates an error occurred while reading
 * the register.
 */
static s32 ad525x_write(struct i2c_client *client, u8 reg, u8 value)
{
	struct dpot_data *data = i2c_get_clientdata(client);

	/* Only write the instruction byte for certain commands */
	if (reg & AD525X_I2C_CMD)
		return i2c_smbus_write_byte(client, reg);

	if (data->max_pos > 256)
		return i2c_smbus_write_word_data(client, (reg & 0xF8) |
						((reg & 0x7) << 1), value);
	else
		/* All other registers require instruction + data bytes */
		return i2c_smbus_write_byte_data(client, reg, value);
}

static int ad525x_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
	struct device *dev = &client->dev;
	struct dpot_data *data;
	int err = 0;

	dev_dbg(dev, "%s\n", __func__);

	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {
		dev_err(dev, "missing I2C functionality for this driver\n");
		goto exit;
	}

	data = kzalloc(sizeof(struct dpot_data), GFP_KERNEL);
	if (!data) {
		err = -ENOMEM;
		goto exit;
	}

	i2c_set_clientdata(client, data);
	mutex_init(&data->update_lock);

	switch (id->driver_data) {
	case AD5258_ID:
		data->max_pos = AD5258_MAX_POSITION;
		err = sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC0]);
		break;
	case AD5259_ID:
		data->max_pos = AD5259_MAX_POSITION;
		err = sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC0]);
		break;
	case AD5251_ID:
		data->max_pos = AD5251_MAX_POSITION;
		err = sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC1]);
		err |= sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC3]);
		err |= sysfs_create_group(&dev->kobj, &ad525x_group_commands);
		break;
	case AD5252_ID:
		data->max_pos = AD5252_MAX_POSITION;
		err = sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC1]);
		err |= sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC3]);
		err |= sysfs_create_group(&dev->kobj, &ad525x_group_commands);
		break;
	case AD5253_ID:
		data->max_pos = AD5253_MAX_POSITION;
		err = sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC0]);
		err |= sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC1]);
		err |= sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC2]);
		err |= sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC3]);
		err |= sysfs_create_group(&dev->kobj, &ad525x_group_commands);
		break;
	case AD5254_ID:
		data->max_pos = AD5254_MAX_POSITION;
		err = sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC0]);
		err |= sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC1]);
		err |= sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC2]);
		err |= sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC3]);
		err |= sysfs_create_group(&dev->kobj, &ad525x_group_commands);
		break;
	case AD5255_ID:
		data->max_pos = AD5255_MAX_POSITION;
		err = sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC0]);
		err |= sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC1]);
		err |= sysfs_create_group(&dev->kobj,
				       &ad525x_group_wipers[AD525X_RDAC2]);
		err |= sysfs_create_group(&dev->kobj, &ad525x_group_commands);
		break;
	default:
		err = -ENODEV;
		goto exit_free;
	}

	if (err) {
		dev_err(dev, "failed to register sysfs hooks\n");
		goto exit_free;
	}

	data->devid = id->driver_data;
	data->rdac_mask = data->max_pos - 1;

	dev_info(dev, "%s %d-Position Digital Potentiometer registered\n",
		 id->name, data->max_pos);

	return 0;

exit_free:
	kfree(data);
	i2c_set_clientdata(client, NULL);
exit:
	dev_err(dev, "failed to create client\n");
	return err;
}

static int __devexit ad525x_remove(struct i2c_client *client)
{
	struct dpot_data *data = i2c_get_clientdata(client);
	struct device *dev = &client->dev;

	switch (data->devid) {
	case AD5258_ID:
	case AD5259_ID:
		sysfs_remove_group(&dev->kobj,
				   &ad525x_group_wipers[AD525X_RDAC0]);
		break;
	case AD5251_ID:
	case AD5252_ID:
		sysfs_remove_group(&dev->kobj,
				   &ad525x_group_wipers[AD525X_RDAC1]);
		sysfs_remove_group(&dev->kobj,
				   &ad525x_group_wipers[AD525X_RDAC3]);
		sysfs_remove_group(&dev->kobj, &ad525x_group_commands);
		break;
	case AD5253_ID:
	case AD5254_ID:
		sysfs_remove_group(&dev->kobj,
				   &ad525x_group_wipers[AD525X_RDAC0]);
		sysfs_remove_group(&dev->kobj,
				   &ad525x_group_wipers[AD525X_RDAC1]);
		sysfs_remove_group(&dev->kobj,
				   &ad525x_group_wipers[AD525X_RDAC2]);
		sysfs_remove_group(&dev->kobj,
				   &ad525x_group_wipers[AD525X_RDAC3]);
		sysfs_remove_group(&dev->kobj, &ad525x_group_commands);
		break;
	case AD5255_ID:
		sysfs_remove_group(&dev->kobj,
				   &ad525x_group_wipers[AD525X_RDAC0]);
		sysfs_remove_group(&dev->kobj,
				   &ad525x_group_wipers[AD525X_RDAC1]);
		sysfs_remove_group(&dev->kobj,
				   &ad525x_group_wipers[AD525X_RDAC2]);
		sysfs_remove_group(&dev->kobj, &ad525x_group_commands);
		break;
	}

	i2c_set_clientdata(client, NULL);
	kfree(data);

	return 0;
}

static const struct i2c_device_id ad525x_idtable[] = {
	{"ad5258", AD5258_ID},
	{"ad5259", AD5259_ID},
	{"ad5251", AD5251_ID},
	{"ad5252", AD5252_ID},
	{"ad5253", AD5253_ID},
	{"ad5254", AD5254_ID},
	{"ad5255", AD5255_ID},
	{}
};

MODULE_DEVICE_TABLE(i2c, ad525x_idtable);

static struct i2c_driver ad525x_driver = {
	.driver = {
		   .owner = THIS_MODULE,
		   .name = DRIVER_NAME,
		   },
	.id_table = ad525x_idtable,
	.probe = ad525x_probe,
	.remove = __devexit_p(ad525x_remove),
};

static int __init ad525x_init(void)
{
	return i2c_add_driver(&ad525x_driver);
}

module_init(ad525x_init);

static void __exit ad525x_exit(void)
{
	i2c_del_driver(&ad525x_driver);
}

module_exit(ad525x_exit);

MODULE_AUTHOR("Chris Verges <chrisv@cyberswitching.com>, "
	      "Michael Hennerich <hennerich@blackfin.uclinux.org>, ");
MODULE_DESCRIPTION("AD5258/9 digital potentiometer driver");
MODULE_LICENSE("GPL");
MODULE_VERSION(DRIVER_VERSION);