blob: 9b237221bc4e8abf19391886ee6465065db2e16a [file] [log] [blame]
Daniel Mack4d009282009-12-14 18:00:17 -08001/*
2 * dac7512.c - Linux kernel module for
3 * Texas Instruments DAC7512
4 *
5 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/spi/spi.h>
25
26#define DAC7512_DRV_NAME "dac7512"
27#define DRIVER_VERSION "1.0"
28
29static ssize_t dac7512_store_val(struct device *dev,
30 struct device_attribute *attr,
31 const char *buf, size_t count)
32{
33 struct spi_device *spi = to_spi_device(dev);
34 unsigned char tmp[2];
35 unsigned long val;
Jingoo Hanf7b41272013-06-04 13:15:16 +090036 int ret;
Daniel Mack4d009282009-12-14 18:00:17 -080037
Jingoo Hanf7b41272013-06-04 13:15:16 +090038 ret = kstrtoul(buf, 10, &val);
39 if (ret)
40 return ret;
Daniel Mack4d009282009-12-14 18:00:17 -080041
42 tmp[0] = val >> 8;
43 tmp[1] = val & 0xff;
44 spi_write(spi, tmp, sizeof(tmp));
45 return count;
46}
47
48static DEVICE_ATTR(value, S_IWUSR, NULL, dac7512_store_val);
49
50static struct attribute *dac7512_attributes[] = {
51 &dev_attr_value.attr,
52 NULL
53};
54
55static const struct attribute_group dac7512_attr_group = {
56 .attrs = dac7512_attributes,
57};
58
Bill Pemberton80c8ae22012-11-19 13:23:05 -050059static int dac7512_probe(struct spi_device *spi)
Daniel Mack4d009282009-12-14 18:00:17 -080060{
61 int ret;
62
63 spi->bits_per_word = 8;
64 spi->mode = SPI_MODE_0;
65 ret = spi_setup(spi);
66 if (ret < 0)
67 return ret;
68
69 return sysfs_create_group(&spi->dev.kobj, &dac7512_attr_group);
70}
71
Bill Pemberton486a5c22012-11-19 13:26:02 -050072static int dac7512_remove(struct spi_device *spi)
Daniel Mack4d009282009-12-14 18:00:17 -080073{
74 sysfs_remove_group(&spi->dev.kobj, &dac7512_attr_group);
75 return 0;
76}
77
78static struct spi_driver dac7512_driver = {
79 .driver = {
80 .name = DAC7512_DRV_NAME,
81 .owner = THIS_MODULE,
82 },
83 .probe = dac7512_probe,
Bill Pemberton2d6bed92012-11-19 13:21:23 -050084 .remove = dac7512_remove,
Daniel Mack4d009282009-12-14 18:00:17 -080085};
86
Axel Lina3dc3c92012-01-22 15:38:22 +080087module_spi_driver(dac7512_driver);
Daniel Mack4d009282009-12-14 18:00:17 -080088
89MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
90MODULE_DESCRIPTION("DAC7512 16-bit DAC");
91MODULE_LICENSE("GPL v2");
92MODULE_VERSION(DRIVER_VERSION);