blob: 83da711ce9f13688660192f55fac62571961ec93 [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>
Daniel Mackf91f9252013-09-22 21:51:49 +020025#include <linux/of.h>
Daniel Mack4d009282009-12-14 18:00:17 -080026
Daniel Mack4d009282009-12-14 18:00:17 -080027static ssize_t dac7512_store_val(struct device *dev,
28 struct device_attribute *attr,
29 const char *buf, size_t count)
30{
31 struct spi_device *spi = to_spi_device(dev);
32 unsigned char tmp[2];
33 unsigned long val;
Jingoo Hanf7b41272013-06-04 13:15:16 +090034 int ret;
Daniel Mack4d009282009-12-14 18:00:17 -080035
Jingoo Hanf7b41272013-06-04 13:15:16 +090036 ret = kstrtoul(buf, 10, &val);
37 if (ret)
38 return ret;
Daniel Mack4d009282009-12-14 18:00:17 -080039
40 tmp[0] = val >> 8;
41 tmp[1] = val & 0xff;
42 spi_write(spi, tmp, sizeof(tmp));
43 return count;
44}
45
46static DEVICE_ATTR(value, S_IWUSR, NULL, dac7512_store_val);
47
48static struct attribute *dac7512_attributes[] = {
49 &dev_attr_value.attr,
50 NULL
51};
52
53static const struct attribute_group dac7512_attr_group = {
54 .attrs = dac7512_attributes,
55};
56
Bill Pemberton80c8ae22012-11-19 13:23:05 -050057static int dac7512_probe(struct spi_device *spi)
Daniel Mack4d009282009-12-14 18:00:17 -080058{
59 int ret;
60
61 spi->bits_per_word = 8;
62 spi->mode = SPI_MODE_0;
63 ret = spi_setup(spi);
64 if (ret < 0)
65 return ret;
66
67 return sysfs_create_group(&spi->dev.kobj, &dac7512_attr_group);
68}
69
Bill Pemberton486a5c22012-11-19 13:26:02 -050070static int dac7512_remove(struct spi_device *spi)
Daniel Mack4d009282009-12-14 18:00:17 -080071{
72 sysfs_remove_group(&spi->dev.kobj, &dac7512_attr_group);
73 return 0;
74}
75
Daniel Mackbeb900f2013-09-22 21:51:48 +020076static const struct spi_device_id dac7512_id_table[] = {
77 { "dac7512", 0 },
78 { }
79};
80MODULE_DEVICE_TABLE(spi, dac7512_id_table);
81
Daniel Mackf91f9252013-09-22 21:51:49 +020082#ifdef CONFIG_OF
83static const struct of_device_id dac7512_of_match[] = {
84 { .compatible = "ti,dac7512", },
85 { }
86};
87MODULE_DEVICE_TABLE(of, dac7512_of_match);
88#endif
89
Daniel Mack4d009282009-12-14 18:00:17 -080090static struct spi_driver dac7512_driver = {
91 .driver = {
Daniel Macke0646502013-09-22 21:51:47 +020092 .name = "dac7512",
Daniel Mack4d009282009-12-14 18:00:17 -080093 .owner = THIS_MODULE,
Daniel Mackf91f9252013-09-22 21:51:49 +020094 .of_match_table = of_match_ptr(dac7512_of_match),
Daniel Mack4d009282009-12-14 18:00:17 -080095 },
96 .probe = dac7512_probe,
Bill Pemberton2d6bed92012-11-19 13:21:23 -050097 .remove = dac7512_remove,
Daniel Mackbeb900f2013-09-22 21:51:48 +020098 .id_table = dac7512_id_table,
Daniel Mack4d009282009-12-14 18:00:17 -080099};
100
Axel Lina3dc3c92012-01-22 15:38:22 +0800101module_spi_driver(dac7512_driver);
Daniel Mack4d009282009-12-14 18:00:17 -0800102
103MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
104MODULE_DESCRIPTION("DAC7512 16-bit DAC");
105MODULE_LICENSE("GPL v2");