From patchwork Fri Jun 11 10:19:51 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Raffaele Recalcati X-Patchwork-Id: 105549 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 o5BAMpQd017953 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 11 Jun 2010 10:23:27 GMT Received: from dlep36.itg.ti.com ([157.170.170.91]) by comal.ext.ti.com (8.13.7/8.13.7) with ESMTP id o5BAMpCX014614 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Fri, 11 Jun 2010 05:22:51 -0500 Received: from linux.omap.com (localhost [127.0.0.1]) by dlep36.itg.ti.com (8.13.8/8.13.8) with ESMTP id o5BAMprx012131 for ; Fri, 11 Jun 2010 05:22:51 -0500 (CDT) Received: from linux.omap.com (localhost [127.0.0.1]) by linux.omap.com (Postfix) with ESMTP id 5E0E38062B for ; Fri, 11 Jun 2010 05:22:51 -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 C4B9980633 for ; Fri, 11 Jun 2010 05:20:23 -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 o5BAKLIi009383 for ; Fri, 11 Jun 2010 05:20:21 -0500 (CDT) Received: from psmtp.com (na3sys009amx221.postini.com [74.125.149.61]) by medina.ext.ti.com (8.13.7/8.13.7) with SMTP id o5BAKKG0027430 for ; Fri, 11 Jun 2010 05:20:20 -0500 Received: from source ([74.125.82.45]) by na3sys009amx221.postini.com ([74.125.148.13]) with SMTP; Fri, 11 Jun 2010 03:20:20 PDT Received: by mail-ww0-f45.google.com with SMTP id 13so674388wwb.4 for ; Fri, 11 Jun 2010 03:20:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:to:cc:subject:date :message-id:x-mailer:in-reply-to:references; bh=dUj9a8rmfIXbzOsKWd6aVHuqiKauFdW146jn9gQk+OU=; b=L5L7acJNXODvz0RI8vbFbq2LvGObnNkOBF60otuCii2gDY2b8W5OQ/FgmjOdRl7ZTF l7RmjowaIvIFqsD8f98tXsXl8TZAzzd+bYBIcVoXENsGZ8g32iHM47y6nuIDd0JT00Hx 0bPm8LGeAEp7n97mLFOCQhMM5rOzY3OU+smtg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=OnsKM4KD2BUvYXibNy7Or/UVM8asoZw905bYNtX9l8CDSeJbWKyAapZHCRMWDyoYH9 AZt1/E+PfFU88760iJ0Loy7LkrQXT49NIaOu5HGpGd6fMp67pqbeMgniV22Kd5xrOYbc UWq/Tayv57pzNcp20cMHu+zghEMljnnxeup1E= Received: by 10.227.143.66 with SMTP id t2mr1595507wbu.116.1276251619869; Fri, 11 Jun 2010 03:20:19 -0700 (PDT) Received: from localhost.localdomain (host81-90-static.72-81-b.business.telecomitalia.it [81.72.90.81]) by mx.google.com with ESMTPS id p17sm530613wbe.14.2010.06.11.03.20.17 (version=TLSv1/SSLv3 cipher=RC4-MD5); Fri, 11 Jun 2010 03:20:18 -0700 (PDT) From: Raffaele Recalcati To: davinci-linux-open-source@linux.davincidsp.com Subject: [PATCH 11/14] regulator: add support for Dallas DS1803 dual digital potentiometer Date: Fri, 11 Jun 2010 12:19:51 +0200 Message-Id: <1276251594-5936-11-git-send-email-lamiaposta71@gmail.com> X-Mailer: git-send-email 1.7.0.4 In-Reply-To: <1276251594-5936-1-git-send-email-lamiaposta71@gmail.com> References: <1276251594-5936-1-git-send-email-lamiaposta71@gmail.com> X-pstn-neptune: 0/0/0.00/0 X-pstn-levels: (S:11.58998/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] Cc: Rodolfo Giometti 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: , MIME-Version: 1.0 Sender: davinci-linux-open-source-bounces+patchwork-davinci=patchwork.kernel.org@linux.davincidsp.com Errors-To: davinci-linux-open-source-bounces+patchwork-davinci=patchwork.kernel.org@linux.davincidsp.com X-Greylist: Sender succeeded STARTTLS authentication, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Fri, 11 Jun 2010 10:23:28 +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]; +}; + +#endif /* _DS1803_H */