From patchwork Mon Jun 7 10:16:24 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Raffaele Recalcati X-Patchwork-Id: 104686 Received: from comal.ext.ti.com (comal.ext.ti.com [198.47.26.152]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o57ALVJH010013 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 7 Jun 2010 10:22:07 GMT Received: from dlep35.itg.ti.com ([157.170.170.118]) by comal.ext.ti.com (8.13.7/8.13.7) with ESMTP id o57AJYf1017098 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 7 Jun 2010 05:19:34 -0500 Received: from linux.omap.com (localhost [127.0.0.1]) by dlep35.itg.ti.com (8.13.7/8.13.7) with ESMTP id o57AJXfI009534; Mon, 7 Jun 2010 05:19:33 -0500 (CDT) Received: from linux.omap.com (localhost [127.0.0.1]) by linux.omap.com (Postfix) with ESMTP id 8395780631; Mon, 7 Jun 2010 05:19:33 -0500 (CDT) X-Original-To: davinci-linux-open-source@linux.davincidsp.com Delivered-To: davinci-linux-open-source@linux.davincidsp.com Received: from dflp52.itg.ti.com (dflp52.itg.ti.com [128.247.22.96]) by linux.omap.com (Postfix) with ESMTP id DB1FC80628 for ; Mon, 7 Jun 2010 05:16:26 -0500 (CDT) Received: from medina.ext.ti.com (localhost [127.0.0.1]) by dflp52.itg.ti.com (8.13.7/8.13.7) with ESMTP id o57AGQWl026940 for ; Mon, 7 Jun 2010 05:16:26 -0500 (CDT) Received: from psmtp.com (na3sys009amx231.postini.com [74.125.149.115]) by medina.ext.ti.com (8.13.7/8.13.7) with SMTP id o57AGPsJ024197 for ; Mon, 7 Jun 2010 05:16:25 -0500 Received: from source ([209.85.214.173]) by na3sys009amx231.postini.com ([74.125.148.10]) with SMTP; Mon, 07 Jun 2010 10:16:26 GMT Received: by mail-iw0-f173.google.com with SMTP id 41so3308744iwn.4 for ; Mon, 07 Jun 2010 03:16:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:date:message-id :subject:from:to:content-type; bh=4JWZ2GWAmBzQBbMbtbQaYi3a05dfJT2jP0iHEUBiF3U=; b=Ndy1PZnBwu2GMU2V8krWUbCxZZpDd6j6kMBe0zLfuH6uP4jOUL1Hgn5TKFgFv2/J3k ucfNqJiDbil0N2ZraTR/jiqBvIHv/cAvBa4U7qCMzHG3JPQMlHezVwnDCItbmraSynww J4mq3lU6ZqAMAWRbxT8ZnaWaOsOpmZWZ36ZgQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=P92ETPPKQgDCG07uh4gTUdD+3208hjZgqKPgbJUIyTqRxKbi+rxcTe4ElkpMnUklvx rfbQh2VMUb48u9TOZZ/jOGpP96q6RCkvMaPnnTJV7VpquLG1I0F+7SXydR+IjNUjp5u0 ScPsfNBY9c4c56UgXfHrJKhFUnHJ5Tq6OXLko= MIME-Version: 1.0 Received: by 10.231.120.76 with SMTP id c12mr17055026ibr.92.1275905784780; Mon, 07 Jun 2010 03:16:24 -0700 (PDT) Received: by 10.231.172.16 with HTTP; Mon, 7 Jun 2010 03:16:24 -0700 (PDT) Date: Mon, 7 Jun 2010 12:16:24 +0200 Message-ID: Subject: [PATCH 12/12] regulator: add support for Dallas DS1803 dual digital potentiometer From: Raffaele Recalcati To: davinci-linux-open-source X-pstn-neptune: 0/0/0.00/0 X-pstn-levels: (S: 2.51578/99.90000 CV:99.9000 FC:95.5390 LC:95.5390 R:95.9108 P:95.9108 M:97.0282 C:98.6951 ) X-pstn-settings: 2 (0.5000:0.5000) s cv gt3 gt2 gt1 r p m c X-pstn-addresses: from [db-null] X-BeenThere: davinci-linux-open-source@linux.davincidsp.com X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: davinci-linux-open-source-bounces@linux.davincidsp.com Errors-To: davinci-linux-open-source-bounces@linux.davincidsp.com X-Greylist: Sender succeeded STARTTLS authentication, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Mon, 07 Jun 2010 10:22:08 +0000 (UTC) diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig index 04f2e08..ebaf09b 100644 --- a/drivers/regulator/Kconfig +++ b/drivers/regulator/Kconfig @@ -201,5 +201,12 @@ config REGULATOR_88PM8607 help This driver supports 88PM8607 voltage regulator chips. +config REGULATOR_DS1803 + tristate "Dallas Maxim DS1803 addressable dual digital potentiometer" + depends on I2C + help + Say Y here to support the dual digital potentiometer on + Dallas Maxim DS1803 + endif diff --git a/drivers/regulator/Makefile b/drivers/regulator/Makefile index 4e7feec..c0fe050 100644 --- a/drivers/regulator/Makefile +++ b/drivers/regulator/Makefile @@ -31,5 +31,6 @@ obj-$(CONFIG_REGULATOR_AB3100) += ab3100.o obj-$(CONFIG_REGULATOR_TPS65023) += tps65023-regulator.o obj-$(CONFIG_REGULATOR_TPS6507X) += tps6507x-regulator.o obj-$(CONFIG_REGULATOR_88PM8607) += 88pm8607.o +obj-$(CONFIG_REGULATOR_DS1803) += ds1803.o ccflags-$(CONFIG_REGULATOR_DEBUG) += -DDEBUG diff --git a/drivers/regulator/ds1803.c b/drivers/regulator/ds1803.c new file mode 100644 index 0000000..8621a59 --- /dev/null +++ b/drivers/regulator/ds1803.c @@ -0,0 +1,204 @@ +/* + * ds1803.c -- Voltage regulation for the DS1803 + * + * Copyright (C) 2009 Rodolfo Giometti + * Copyright (C) 2009 Bticino S.p.A. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include +#include + +struct ds1803_data { + struct i2c_client *client; + struct regulator_dev *rdev; + + unsigned int min_uV; /* voltage for selector value 0 */ + unsigned int max_uV; /* voltage for selector value 255 */ + unsigned int init_uV; /* initial voltage */ +}; + +/* + * Low level functions + */ + +static u8 ds1803_write_pot_lut[] = { 0xa9, 0xaa, /* both not supported */ }; + +static int ds1803_set_voltage(struct regulator_dev *rdev, + int min_uV, int max_uV) +{ + struct ds1803_data *ds1803 = rdev_get_drvdata(rdev); + struct i2c_client *client = ds1803->client; + unsigned int id = rdev_get_id(rdev); + int val; + u8 buf[2] = { ds1803_write_pot_lut[id], 0 }; + struct i2c_msg msgs[1] = { + { client->addr , 0, 2, buf }, + }; + int ret; + + + val = (min_uV - ds1803->min_uV) * 255 / ds1803->max_uV; + + pr_notice("%s Writing %d to pot %d\n",__func__, val,id); + + if (val < 0 || val > 255) + return -EINVAL; + buf[1] = val; + + ret = i2c_transfer(client->adapter, &msgs[0], 1); + if (ret != 1) { + dev_err(&client->dev, "%s: write error\n", __func__); + return -EIO; + } + + return 0; +} + +static int ds1803_list_voltage(struct regulator_dev *rdev, unsigned index) +{ + struct ds1803_data *ds1803 = rdev_get_drvdata(rdev); + + return (ds1803->max_uV - ds1803->min_uV) * index / 255 + + ds1803->min_uV; +} + +/* + * Regulator methods + */ + +static struct regulator_ops ds1803_voltage_ops = { + .set_voltage = ds1803_set_voltage, + .list_voltage = ds1803_list_voltage, +}; + +static char *ds1803_names[] = { + [DS1803_100K] = "trimmer_100k", + [DS1803_50K] = "trimmer_50k", + [DS1803_10K] = "trimmer_10k", +}; + +static struct regulator_desc ds1803_reg[] = { + { + .type = REGULATOR_VOLTAGE, + .id = 0, + .n_voltages = 256, + .ops = &ds1803_voltage_ops, + .owner = THIS_MODULE, + }, + { + .type = REGULATOR_VOLTAGE, + .id = 1, + .n_voltages = 256, + .ops = &ds1803_voltage_ops, + .owner = THIS_MODULE, + }, +}; + +static int ds1803_i2c_probe(struct i2c_client *client, + const struct i2c_device_id *i2c_id) +{ + struct ds1803_platform_data *pdata = client->dev.platform_data; + struct ds1803_data *ds1803; + int i, ret = -ENOMEM; + + ds1803 = kzalloc(sizeof(struct ds1803_data) * 2, GFP_KERNEL); + if (!ds1803) + goto out; + + for (i = 0; i < 2; i++) { + ds1803[i].client = client; + + /* No init_data -> no regulation */ + if (!pdata->subdev[i].init) + continue; + + /* Set chip's name according to user supplied type */ + ds1803_reg[i].name = ds1803_names[pdata->type]; + + ds1803[i].min_uV = pdata->subdev[i].min_uV; + ds1803[i].max_uV = pdata->subdev[i].max_uV; + ds1803[i].rdev = regulator_register(&ds1803_reg[i], + &client->dev, + pdata->subdev[i].init, &ds1803[i]); + if (IS_ERR(ds1803[i].rdev)) { + dev_err(&client->dev, + "failed to register regulator %d\n", i); + goto err; + } + } + + i2c_set_clientdata(client, ds1803); + dev_info(&client->dev, "DS1803 regulator driver loaded\n"); + return 0; + +err: + while (--i >= 0) + regulator_unregister(ds1803[i].rdev); + kfree(ds1803); +out: + return ret; +} +static int ds1803_i2c_remove(struct i2c_client *client) +{ + struct ds1803_data *ds1803 = i2c_get_clientdata(client); + int i; + + for (i = 0; i < 2; i++) + if (ds1803[i].rdev) + regulator_unregister(ds1803[i].rdev); + kfree(ds1803); + i2c_set_clientdata(client, NULL); + + return 0; +} + +static const struct i2c_device_id ds1803_id[] = { + { "ds1803", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, ds1803_id); + +static struct i2c_driver ds1803_i2c_driver = { + .probe = ds1803_i2c_probe, + .remove = ds1803_i2c_remove, + .driver = { + .name = "ds1803", + }, + .id_table = ds1803_id, +}; + +static int __init ds1803_i2c_init(void) +{ + return i2c_add_driver(&ds1803_i2c_driver); +} +subsys_initcall(ds1803_i2c_init); + +static void __exit ds1803_i2c_exit(void) +{ + i2c_del_driver(&ds1803_i2c_driver); +} +module_exit(ds1803_i2c_exit); + +/* Module information */ +MODULE_DESCRIPTION("DS1803 voltage regulator driver"); +MODULE_AUTHOR("Rodolfo Giometti "); +MODULE_LICENSE("GPL v2"); diff --git a/include/linux/regulator/ds1803.h b/include/linux/regulator/ds1803.h new file mode 100644 index 0000000..6bce0ec --- /dev/null +++ b/include/linux/regulator/ds1803.h @@ -0,0 +1,41 @@ +/* + * ds1803.h -- Voltage regulation for the DS1803 + * + * Copyright (C) 2009 Rodolfo Giometti + * Copyright (C) 2009 Bticino S.p.A. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation version 2. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _DS1803_H +#define _DS1803_H + +#include + +enum ds1803_type { + DS1803_100K, + DS1803_50K, + DS1803_10K, +}; + +struct ds1803_platform_data { + enum ds1803_type type; + struct { + unsigned int min_uV; + unsigned int max_uV; + struct regulator_init_data *init; + } subdev[2]; +}; +