mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@cam.ac.uk>
To: linux-kernel@vger.kernel.org
Cc: chrisv@cyberswitching.com, daniel@caiaq.de, iws@ovro.caltech.edu,
	dmitry.torokhov@gmail.com, hmh@hmh.eng.br, david-b@pacbell.net,
	khali@linux-fr.org, Jonathan Cameron <jic23@cam.ac.uk>
Subject: [PATCH 03/14] IIO: tsl2561 digital light sensor core support
Date: Thu,  2 Jul 2009 13:47:33 +0000	[thread overview]
Message-ID: <1246542464-6974-4-git-send-email-jic23@cam.ac.uk> (raw)
In-Reply-To: <1246542464-6974-3-git-send-email-jic23@cam.ac.uk>

Signed-off-by: Jonathan Cameron <jic23@cam.ac.uk>

This is a pretty minimalist example of an IIO driver.

---
Unlike the existing light sensor driver tsl2550 all
conversion to units is left to userspace.
This is similar to the isl29003 driver.

IIO may in future provide a home for such light sensor
drivers.
---
 drivers/iio/Kconfig         |    3 +-
 drivers/iio/Makefile        |    3 +-
 drivers/iio/light/Kconfig   |   14 ++
 drivers/iio/light/Makefile  |    5 +
 drivers/iio/light/tsl2561.c |  275 +++++++++++++++++++++++++++++++++++++++++++
 include/linux/iio/light.h   |   12 ++
 6 files changed, 310 insertions(+), 2 deletions(-)
 create mode 100644 drivers/iio/light/Kconfig
 create mode 100644 drivers/iio/light/Makefile
 create mode 100644 drivers/iio/light/tsl2561.c
 create mode 100644 include/linux/iio/light.h

diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
index ca54ab3..d37859d 100644
--- a/drivers/iio/Kconfig
+++ b/drivers/iio/Kconfig
@@ -10,4 +10,5 @@ menuconfig IIO
 	  number of different physical interfaces (i2c, spi etc). See
 	  Documentation/industrialio for more information.
 
-source drivers/iio/adc/Kconfig
\ No newline at end of file
+source drivers/iio/adc/Kconfig
+source drivers/iio/light/Kconfig
diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
index 9d11f64..4e78f98 100644
--- a/drivers/iio/Makefile
+++ b/drivers/iio/Makefile
@@ -5,4 +5,5 @@
 obj-$(CONFIG_IIO) += industrialio.o
 industrialio-y := industrialio-core.o
 
-obj-y += adc/
\ No newline at end of file
+obj-y += adc/
+obj-y += light/
\ No newline at end of file
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
new file mode 100644
index 0000000..620e249
--- /dev/null
+++ b/drivers/iio/light/Kconfig
@@ -0,0 +1,14 @@
+#
+# Light sensors
+#
+comment "Light sensors"
+	depends on IIO
+
+config TSL2561
+       tristate "TAOS TSL2561 light-to-digital convertor"
+       depends on IIO
+       help
+	Say yes bere to build support for the TAOS light to digital
+	convertor.  This chip has two light sensors. One is broadband
+	including infrared whilst the other measures only infrared.
+	Provides direct access via sysfs.
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
new file mode 100644
index 0000000..ccff151
--- /dev/null
+++ b/drivers/iio/light/Makefile
@@ -0,0 +1,5 @@
+#
+# Makefile for industrial I/O Light sensors
+#
+
+obj-$(CONFIG_TSL2561)   += tsl2561.o
diff --git a/drivers/iio/light/tsl2561.c b/drivers/iio/light/tsl2561.c
new file mode 100644
index 0000000..db1443c
--- /dev/null
+++ b/drivers/iio/light/tsl2561.c
@@ -0,0 +1,275 @@
+/*
+ *  tsl2561.c - Linux kernel modules for light to digital convertor
+ *
+ *  Copyright (C) 2008-2009 Jonathan Cameron <jic23@cam.ac.uk>
+ *
+ *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ *  Some portions based upon the tsl2550 driver.
+ *
+ *  This driver could probably be adapted easily to talk to the tsl2560 (smbus)
+ *
+ *  Needs some work to support the events this can generate.
+ *  Todo: Implement interrupt handling.  Currently a hardware bug means
+ *  this isn't available on my test board.
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/light.h>
+
+#define TSL2561_CONTROL_REGISTER 0x00
+#define TSL2561_TIMING_REGISTER 0x01
+#define TSL2561_THRESHLOW_LOW_REGISTER 0x02
+#define TSL2561_THRESHLOW_HIGH_REGISTER 0x03
+#define TSL2561_THRESHHIGH_LOW_REGISTER 0x04
+#define TSL2561_THRESHHIGH_HIGH_REGISTER 0x05
+#define TSL2561_INT_CONTROL_REGISTER 0x06
+
+#define TSL2561_INT_REG_INT_OFF 0x00
+#define TSL2561_INT_REG_INT_LEVEL 0x08
+#define TSL2561_INT_REG_INT_SMBUS 0x10
+#define TSL2561_INT_REG_INT_TEST 0x18
+
+#define TSL2561_ID_REGISTER 0x0A
+
+#define TSL2561_DATA_0_LOW 0x0C
+#define TSL2561_DATA_1_LOW 0x0E
+
+/* Control Register Values */
+#define TSL2561_CONT_REG_PWR_ON 0x03
+#define TSL2561_CONT_REG_PWR_OFF 0x00
+
+/**
+ * struct tsl2561_state - device specific state
+ * @indio_dev:		the industrialio I/O info structure
+ * @client:		i2c client
+ * @command_buf:	single command buffer used for all operations
+ * @command_buf_lock:	ensure unique access to command_buf
+ */
+struct tsl2561_state {
+	struct iio_dev		*indio_dev;
+	struct i2c_client	*client;
+	struct tsl2561_command	*command_buf;
+	struct mutex		command_buf_lock;
+};
+
+/**
+ * struct tsl2561_command - command byte for smbus
+ * @address:	register address
+ * @block:	is this a block r/w
+ * @word:	is this a word r/w
+ * @clear:	set to 1 to clear pending interrupt
+ * @cmd:	select the command register - always 1.
+ */
+struct tsl2561_command {
+	unsigned int address:4;
+	unsigned int block:1;
+	unsigned int word:1;
+	unsigned int clear:1;
+	unsigned int cmd:1;
+};
+
+static inline void tsl2561_init_command_buf(struct tsl2561_command *buf)
+{
+	buf->address = 0;
+	buf->block = 0;
+	buf->word = 0;
+	buf->clear = 0;
+	buf->cmd = 1;
+}
+
+static ssize_t tsl2561_read_val(struct device *dev,
+				struct device_attribute *attr,
+				char *buf)
+{
+	int ret = 0, data;
+	ssize_t len = 0;
+	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2561_state *st = indio_dev->dev_data;
+
+	mutex_lock(&st->command_buf_lock);
+	st->command_buf->cmd = 1;
+	st->command_buf->word = 1;
+	st->command_buf->address = this_attr->address;
+
+	data = i2c_smbus_read_word_data(st->client, *(char *)(st->command_buf));
+	if (data < 0) {
+		ret = data;
+		goto error_ret;
+	}
+	len = sprintf(buf, "%u\n", data);
+
+error_ret:
+	mutex_unlock(&st->command_buf_lock);
+
+	return ret ? ret : len;
+}
+
+static IIO_DEV_ATTR_LIGHT_INFRARED(0, tsl2561_read_val, TSL2561_DATA_0_LOW);
+static IIO_DEV_ATTR_LIGHT_BROAD(0, tsl2561_read_val, TSL2561_DATA_1_LOW);
+
+static struct attribute *tsl2561_attributes[] = {
+	&iio_dev_attr_light_infrared0.dev_attr.attr,
+	&iio_dev_attr_light_broadspectrum0.dev_attr.attr,
+	NULL,
+};
+
+static const struct attribute_group tsl2561_attribute_group = {
+	.attrs = tsl2561_attributes,
+};
+
+static int tsl2561_initialize(struct tsl2561_state *st)
+{
+	int err;
+
+	mutex_lock(&st->command_buf_lock);
+	st->command_buf->word = 0;
+	st->command_buf->block = 0;
+	st->command_buf->address = TSL2561_CONTROL_REGISTER;
+	err = i2c_smbus_write_byte_data(st->client, *(char *)(st->command_buf),
+					TSL2561_CONT_REG_PWR_ON);
+	if (err)
+		goto error_ret;
+
+	st->command_buf->address = TSL2561_INT_CONTROL_REGISTER;
+	err = i2c_smbus_write_byte_data(st->client, *(char *)(st->command_buf),
+					TSL2561_INT_REG_INT_TEST);
+
+error_ret:
+	mutex_unlock(&st->command_buf_lock);
+
+	return err;
+}
+
+static int tsl2561_powerdown(struct i2c_client *client)
+{
+	int err;
+	struct tsl2561_command Command = {
+		.cmd =  1,
+		.clear = 0,
+		.word = 0,
+		.block = 0,
+		.address = TSL2561_CONTROL_REGISTER,
+	};
+
+	err = i2c_smbus_write_byte_data(client, *(char *)(&Command),
+					TSL2561_CONT_REG_PWR_OFF);
+	return (err < 0) ? err : 0;
+}
+static int __devinit tsl2561_probe(struct i2c_client *client,
+				   const struct i2c_device_id *id)
+{
+	int ret = 0;
+	struct tsl2561_state *st = kzalloc(sizeof(*st), GFP_KERNEL);
+
+	if (st == NULL) {
+		ret = -ENOMEM;
+		goto error_ret;
+	}
+	i2c_set_clientdata(client, st);
+	st->client = client;
+	mutex_init(&st->command_buf_lock);
+
+	st->command_buf = kmalloc(sizeof(*st->command_buf), GFP_KERNEL);
+	if (st->command_buf == NULL) {
+		ret = -ENOMEM;
+		goto error_free_state;
+	}
+	tsl2561_init_command_buf(st->command_buf);
+
+	st->indio_dev = iio_allocate_device();
+	if (st->indio_dev == NULL) {
+		ret = -ENOMEM;
+		goto error_free_command_buf;
+	}
+	st->indio_dev->attrs = &tsl2561_attribute_group;
+	st->indio_dev->dev.parent = &client->dev;
+	st->indio_dev->dev_data = (void *)(st);
+	st->indio_dev->driver_module = THIS_MODULE;
+	st->indio_dev->modes = INDIO_DIRECT_MODE;
+	ret = iio_device_register(st->indio_dev);
+	if (ret)
+		goto error_free_iiodev;
+
+	/* Intialize the chip */
+	ret = tsl2561_initialize(st);
+	if (ret)
+		goto error_unregister_iiodev;
+
+	return 0;
+error_unregister_iiodev:
+	iio_device_unregister(st->indio_dev);
+error_free_iiodev:
+	iio_free_device(st->indio_dev);
+error_free_command_buf:
+	kfree(st->command_buf);
+error_free_state:
+	kfree(st);
+error_ret:
+	return ret;
+
+}
+
+static int __devexit tsl2561_remove(struct i2c_client *client)
+{
+	struct tsl2561_state *st =  i2c_get_clientdata(client);
+
+	iio_device_unregister(st->indio_dev);
+	iio_free_device(st->indio_dev);
+	kfree(st);
+
+	return tsl2561_powerdown(client);
+}
+
+static unsigned short normal_i2c[] = { 0x29, 0x39, 0x49, I2C_CLIENT_END };
+
+I2C_CLIENT_INSMOD;
+
+static const struct i2c_device_id tsl2561_id[] = {
+	{ "tsl2561", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, tsl2561_id);
+
+
+static struct i2c_driver tsl2561_driver = {
+	.driver = {
+		.name = "tsl2561",
+	},
+	.probe = tsl2561_probe,
+	.remove = __devexit_p(tsl2561_remove),
+	.id_table  = tsl2561_id,
+};
+
+static __init int tsl2561_init(void)
+{
+	return i2c_add_driver(&tsl2561_driver);
+}
+module_init(tsl2561_init);
+
+static __exit void tsl2561_exit(void)
+{
+	i2c_del_driver(&tsl2561_driver);
+}
+module_exit(tsl2561_exit);
+
+MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
+MODULE_DESCRIPTION("TSL2561 light sensor driver");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/iio/light.h b/include/linux/iio/light.h
new file mode 100644
index 0000000..60a8640
--- /dev/null
+++ b/include/linux/iio/light.h
@@ -0,0 +1,12 @@
+#include <linux/iio/sysfs.h>
+
+/* Light to digital sensor attributes */
+
+#define IIO_DEV_ATTR_LIGHT_INFRARED(_num, _show, _addr)			\
+	IIO_DEVICE_ATTR(light_infrared##_num, S_IRUGO, _show, NULL, _addr)
+
+#define IIO_DEV_ATTR_LIGHT_BROAD(_num, _show, _addr)			\
+	IIO_DEVICE_ATTR(light_broadspectrum##_num, S_IRUGO, _show, NULL, _addr)
+
+#define IIO_DEV_ATTR_LIGHT_VISIBLE(_num, _show, _addr)			\
+	IIO_DEVICE_ATTR(light_visible##_num, S_IRUGO, _show, NULL, _addr)
-- 
1.6.2


  reply	other threads:[~2009-07-02 13:49 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-02 13:47 [PATCH 00/14] IIO V4. Industrial I/O Subsystem Jonathan Cameron
2009-07-02 13:47 ` [PATCH 01/14] IIO: core support for device registration and management Jonathan Cameron
2009-07-02 13:47   ` [PATCH 02/14] IIO: max1363 ADC driver Jonathan Cameron
2009-07-02 13:47     ` Jonathan Cameron [this message]
2009-07-02 13:47       ` [PATCH 04/14] IIO: lis3l02dq accelerometer core support Jonathan Cameron
2009-07-02 13:47         ` [PATCH 05/14] IIO: kxsd9 accelerometer minimal support Jonathan Cameron
2009-07-02 13:47           ` [PATCH 06/14] IIO: Add generic ring buffer support to the IIO core Jonathan Cameron
2009-07-02 13:47             ` [PATCH 07/14] IIO: VTI sca3000 series accelerometer driver (spi) Jonathan Cameron
2009-07-02 13:47               ` [PATCH 08/14] IIO: Trigger support added to core Jonathan Cameron
2009-07-02 13:47                 ` [PATCH 09/14] IIO: Ring buffer: Initial pass at rarely locked ring buffer Jonathan Cameron
2009-07-02 13:47                   ` [PATCH 10/14] IIO: lis3l02dq ring buffer and data ready trigger support Jonathan Cameron
2009-07-02 13:47                     ` [PATCH 11/14] IIO: max1363 add software ring buffer support using ring_sw Jonathan Cameron
2009-07-02 13:47                       ` [PATCH 12/14] IIO: Periodic timer based trigger Jonathan Cameron
2009-07-02 13:47                         ` [PATCH 13/14] IIO: Proof of concept gpio trigger Jonathan Cameron
2009-07-02 13:47                           ` [PATCH 14/14] IIO: Initial documentation Jonathan Cameron
2009-07-03 17:12                             ` [PATCH] IIO: Add missing I2C dependencies for max1363 and tsl2561 Jonathan Cameron
2009-07-03 19:13                               ` Jean Delvare
2009-07-04 12:22                                 ` Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1246542464-6974-4-git-send-email-jic23@cam.ac.uk \
    --to=jic23@cam.ac.uk \
    --cc=chrisv@cyberswitching.com \
    --cc=daniel@caiaq.de \
    --cc=david-b@pacbell.net \
    --cc=dmitry.torokhov@gmail.com \
    --cc=hmh@hmh.eng.br \
    --cc=iws@ovro.caltech.edu \
    --cc=khali@linux-fr.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®