Daniel Mack | 4d00928 | 2009-12-14 18:00:17 -0800 | [diff] [blame] | 1 | /* |
| 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> |
Daniel Mack | 4d00928 | 2009-12-14 18:00:17 -0800 | [diff] [blame] | 23 | #include <linux/spi/spi.h> |
Daniel Mack | f91f925 | 2013-09-22 21:51:49 +0200 | [diff] [blame] | 24 | #include <linux/of.h> |
Daniel Mack | 4d00928 | 2009-12-14 18:00:17 -0800 | [diff] [blame] | 25 | |
Daniel Mack | 4d00928 | 2009-12-14 18:00:17 -0800 | [diff] [blame] | 26 | static ssize_t dac7512_store_val(struct device *dev, |
| 27 | struct device_attribute *attr, |
| 28 | const char *buf, size_t count) |
| 29 | { |
| 30 | struct spi_device *spi = to_spi_device(dev); |
| 31 | unsigned char tmp[2]; |
| 32 | unsigned long val; |
Jingoo Han | f7b4127 | 2013-06-04 13:15:16 +0900 | [diff] [blame] | 33 | int ret; |
Daniel Mack | 4d00928 | 2009-12-14 18:00:17 -0800 | [diff] [blame] | 34 | |
Jingoo Han | f7b4127 | 2013-06-04 13:15:16 +0900 | [diff] [blame] | 35 | ret = kstrtoul(buf, 10, &val); |
| 36 | if (ret) |
| 37 | return ret; |
Daniel Mack | 4d00928 | 2009-12-14 18:00:17 -0800 | [diff] [blame] | 38 | |
| 39 | tmp[0] = val >> 8; |
| 40 | tmp[1] = val & 0xff; |
| 41 | spi_write(spi, tmp, sizeof(tmp)); |
| 42 | return count; |
| 43 | } |
| 44 | |
| 45 | static DEVICE_ATTR(value, S_IWUSR, NULL, dac7512_store_val); |
| 46 | |
| 47 | static struct attribute *dac7512_attributes[] = { |
| 48 | &dev_attr_value.attr, |
| 49 | NULL |
| 50 | }; |
| 51 | |
| 52 | static const struct attribute_group dac7512_attr_group = { |
| 53 | .attrs = dac7512_attributes, |
| 54 | }; |
| 55 | |
Bill Pemberton | 80c8ae2 | 2012-11-19 13:23:05 -0500 | [diff] [blame] | 56 | static int dac7512_probe(struct spi_device *spi) |
Daniel Mack | 4d00928 | 2009-12-14 18:00:17 -0800 | [diff] [blame] | 57 | { |
| 58 | int ret; |
| 59 | |
| 60 | spi->bits_per_word = 8; |
| 61 | spi->mode = SPI_MODE_0; |
| 62 | ret = spi_setup(spi); |
| 63 | if (ret < 0) |
| 64 | return ret; |
| 65 | |
| 66 | return sysfs_create_group(&spi->dev.kobj, &dac7512_attr_group); |
| 67 | } |
| 68 | |
Bill Pemberton | 486a5c2 | 2012-11-19 13:26:02 -0500 | [diff] [blame] | 69 | static int dac7512_remove(struct spi_device *spi) |
Daniel Mack | 4d00928 | 2009-12-14 18:00:17 -0800 | [diff] [blame] | 70 | { |
| 71 | sysfs_remove_group(&spi->dev.kobj, &dac7512_attr_group); |
| 72 | return 0; |
| 73 | } |
| 74 | |
Daniel Mack | beb900f | 2013-09-22 21:51:48 +0200 | [diff] [blame] | 75 | static const struct spi_device_id dac7512_id_table[] = { |
| 76 | { "dac7512", 0 }, |
| 77 | { } |
| 78 | }; |
| 79 | MODULE_DEVICE_TABLE(spi, dac7512_id_table); |
| 80 | |
Daniel Mack | f91f925 | 2013-09-22 21:51:49 +0200 | [diff] [blame] | 81 | #ifdef CONFIG_OF |
| 82 | static const struct of_device_id dac7512_of_match[] = { |
| 83 | { .compatible = "ti,dac7512", }, |
| 84 | { } |
| 85 | }; |
| 86 | MODULE_DEVICE_TABLE(of, dac7512_of_match); |
| 87 | #endif |
| 88 | |
Daniel Mack | 4d00928 | 2009-12-14 18:00:17 -0800 | [diff] [blame] | 89 | static struct spi_driver dac7512_driver = { |
| 90 | .driver = { |
Daniel Mack | e064650 | 2013-09-22 21:51:47 +0200 | [diff] [blame] | 91 | .name = "dac7512", |
Daniel Mack | 4d00928 | 2009-12-14 18:00:17 -0800 | [diff] [blame] | 92 | .owner = THIS_MODULE, |
Daniel Mack | f91f925 | 2013-09-22 21:51:49 +0200 | [diff] [blame] | 93 | .of_match_table = of_match_ptr(dac7512_of_match), |
Daniel Mack | 4d00928 | 2009-12-14 18:00:17 -0800 | [diff] [blame] | 94 | }, |
| 95 | .probe = dac7512_probe, |
Bill Pemberton | 2d6bed9 | 2012-11-19 13:21:23 -0500 | [diff] [blame] | 96 | .remove = dac7512_remove, |
Daniel Mack | beb900f | 2013-09-22 21:51:48 +0200 | [diff] [blame] | 97 | .id_table = dac7512_id_table, |
Daniel Mack | 4d00928 | 2009-12-14 18:00:17 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
Axel Lin | a3dc3c9 | 2012-01-22 15:38:22 +0800 | [diff] [blame] | 100 | module_spi_driver(dac7512_driver); |
Daniel Mack | 4d00928 | 2009-12-14 18:00:17 -0800 | [diff] [blame] | 101 | |
| 102 | MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); |
| 103 | MODULE_DESCRIPTION("DAC7512 16-bit DAC"); |
| 104 | MODULE_LICENSE("GPL v2"); |