blob: 566c58bd44d073e32dfff5b01eb457faededab99 [file] [log] [blame]
Atsushi Nemotobc89b2b2008-10-20 23:28:50 +09001/*
2 * 7 Segment LED routines
3 * Based on RBTX49xx patch from CELF patch archive.
4 *
5 * This file is subject to the terms and conditions of the GNU General Public
6 * License. See the file "COPYING" in the main directory of this archive
7 * for more details.
8 *
9 * (C) Copyright TOSHIBA CORPORATION 2005-2007
10 * All Rights Reserved.
11 */
Kay Sievers0e38eaf2011-12-21 15:09:53 -080012#include <linux/device.h>
Atsushi Nemotobc89b2b2008-10-20 23:28:50 +090013#include <linux/slab.h>
14#include <linux/map_to_7segment.h>
15#include <asm/txx9/generic.h>
16
17static unsigned int tx_7segled_num;
18static void (*tx_7segled_putc)(unsigned int pos, unsigned char val);
19
20void __init txx9_7segled_init(unsigned int num,
21 void (*putc)(unsigned int pos, unsigned char val))
22{
23 tx_7segled_num = num;
24 tx_7segled_putc = putc;
25}
26
27static SEG7_CONVERSION_MAP(txx9_seg7map, MAP_ASCII7SEG_ALPHANUM_LC);
28
29int txx9_7segled_putc(unsigned int pos, char c)
30{
31 if (pos >= tx_7segled_num)
32 return -EINVAL;
33 c = map_to_seg7(&txx9_seg7map, c);
34 if (c < 0)
35 return c;
36 tx_7segled_putc(pos, c);
37 return 0;
38}
39
Kay Sievers0e38eaf2011-12-21 15:09:53 -080040static ssize_t ascii_store(struct device *dev,
41 struct device_attribute *attr,
Atsushi Nemotobc89b2b2008-10-20 23:28:50 +090042 const char *buf, size_t size)
43{
44 unsigned int ch = dev->id;
45 txx9_7segled_putc(ch, buf[0]);
46 return size;
47}
48
Kay Sievers0e38eaf2011-12-21 15:09:53 -080049static ssize_t raw_store(struct device *dev,
50 struct device_attribute *attr,
Atsushi Nemotobc89b2b2008-10-20 23:28:50 +090051 const char *buf, size_t size)
52{
53 unsigned int ch = dev->id;
54 tx_7segled_putc(ch, buf[0]);
55 return size;
56}
57
Kay Sievers0e38eaf2011-12-21 15:09:53 -080058static DEVICE_ATTR(ascii, 0200, NULL, ascii_store);
59static DEVICE_ATTR(raw, 0200, NULL, raw_store);
Atsushi Nemotobc89b2b2008-10-20 23:28:50 +090060
Kay Sievers0e38eaf2011-12-21 15:09:53 -080061static ssize_t map_seg7_show(struct device *dev,
62 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +010063 char *buf)
Atsushi Nemotobc89b2b2008-10-20 23:28:50 +090064{
65 memcpy(buf, &txx9_seg7map, sizeof(txx9_seg7map));
66 return sizeof(txx9_seg7map);
67}
68
Kay Sievers0e38eaf2011-12-21 15:09:53 -080069static ssize_t map_seg7_store(struct device *dev,
70 struct device_attribute *attr,
Atsushi Nemotobc89b2b2008-10-20 23:28:50 +090071 const char *buf, size_t size)
72{
73 if (size != sizeof(txx9_seg7map))
74 return -EINVAL;
75 memcpy(&txx9_seg7map, buf, size);
76 return size;
77}
78
Kay Sievers0e38eaf2011-12-21 15:09:53 -080079static DEVICE_ATTR(map_seg7, 0600, map_seg7_show, map_seg7_store);
Atsushi Nemotobc89b2b2008-10-20 23:28:50 +090080
Kay Sievers0e38eaf2011-12-21 15:09:53 -080081static struct bus_type tx_7segled_subsys = {
82 .name = "7segled",
83 .dev_name = "7segled",
Atsushi Nemotobc89b2b2008-10-20 23:28:50 +090084};
85
Levente Kurusaf0a7a2d2013-12-19 16:03:26 +010086static void tx_7segled_release(struct device *dev)
87{
88 kfree(dev);
89}
90
Atsushi Nemotobc89b2b2008-10-20 23:28:50 +090091static int __init tx_7segled_init_sysfs(void)
92{
93 int error, i;
94 if (!tx_7segled_num)
95 return -ENODEV;
Kay Sievers0e38eaf2011-12-21 15:09:53 -080096 error = subsys_system_register(&tx_7segled_subsys, NULL);
Atsushi Nemotobc89b2b2008-10-20 23:28:50 +090097 if (error)
98 return error;
Kay Sievers0e38eaf2011-12-21 15:09:53 -080099 error = device_create_file(tx_7segled_subsys.dev_root, &dev_attr_map_seg7);
Atsushi Nemotobc89b2b2008-10-20 23:28:50 +0900100 if (error)
101 return error;
102 for (i = 0; i < tx_7segled_num; i++) {
Kay Sievers0e38eaf2011-12-21 15:09:53 -0800103 struct device *dev;
Atsushi Nemotobc89b2b2008-10-20 23:28:50 +0900104 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
105 if (!dev) {
106 error = -ENODEV;
107 break;
108 }
109 dev->id = i;
Yoichi Yuasa193fb422012-01-10 21:11:56 +0900110 dev->bus = &tx_7segled_subsys;
Levente Kurusaf0a7a2d2013-12-19 16:03:26 +0100111 dev->release = &tx_7segled_release;
Kay Sievers0e38eaf2011-12-21 15:09:53 -0800112 error = device_register(dev);
Levente Kurusaf0a7a2d2013-12-19 16:03:26 +0100113 if (error) {
114 put_device(dev);
115 return error;
Atsushi Nemotobc89b2b2008-10-20 23:28:50 +0900116 }
Levente Kurusaf0a7a2d2013-12-19 16:03:26 +0100117 device_create_file(dev, &dev_attr_ascii);
118 device_create_file(dev, &dev_attr_raw);
Atsushi Nemotobc89b2b2008-10-20 23:28:50 +0900119 }
120 return error;
121}
122
123device_initcall(tx_7segled_init_sysfs);