1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * 7 Segment LED routines
3*4882a593Smuzhiyun * Based on RBTX49xx patch from CELF patch archive.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This file is subject to the terms and conditions of the GNU General Public
6*4882a593Smuzhiyun * License. See the file "COPYING" in the main directory of this archive
7*4882a593Smuzhiyun * for more details.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * (C) Copyright TOSHIBA CORPORATION 2005-2007
10*4882a593Smuzhiyun * All Rights Reserved.
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun #include <linux/device.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun #include <linux/map_to_7segment.h>
15*4882a593Smuzhiyun #include <asm/txx9/generic.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun static unsigned int tx_7segled_num;
18*4882a593Smuzhiyun static void (*tx_7segled_putc)(unsigned int pos, unsigned char val);
19*4882a593Smuzhiyun
txx9_7segled_init(unsigned int num,void (* putc)(unsigned int pos,unsigned char val))20*4882a593Smuzhiyun void __init txx9_7segled_init(unsigned int num,
21*4882a593Smuzhiyun void (*putc)(unsigned int pos, unsigned char val))
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun tx_7segled_num = num;
24*4882a593Smuzhiyun tx_7segled_putc = putc;
25*4882a593Smuzhiyun }
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun static SEG7_CONVERSION_MAP(txx9_seg7map, MAP_ASCII7SEG_ALPHANUM_LC);
28*4882a593Smuzhiyun
txx9_7segled_putc(unsigned int pos,char c)29*4882a593Smuzhiyun int txx9_7segled_putc(unsigned int pos, char c)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun if (pos >= tx_7segled_num)
32*4882a593Smuzhiyun return -EINVAL;
33*4882a593Smuzhiyun c = map_to_seg7(&txx9_seg7map, c);
34*4882a593Smuzhiyun if (c < 0)
35*4882a593Smuzhiyun return c;
36*4882a593Smuzhiyun tx_7segled_putc(pos, c);
37*4882a593Smuzhiyun return 0;
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun
ascii_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)40*4882a593Smuzhiyun static ssize_t ascii_store(struct device *dev,
41*4882a593Smuzhiyun struct device_attribute *attr,
42*4882a593Smuzhiyun const char *buf, size_t size)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun unsigned int ch = dev->id;
45*4882a593Smuzhiyun txx9_7segled_putc(ch, buf[0]);
46*4882a593Smuzhiyun return size;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
raw_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)49*4882a593Smuzhiyun static ssize_t raw_store(struct device *dev,
50*4882a593Smuzhiyun struct device_attribute *attr,
51*4882a593Smuzhiyun const char *buf, size_t size)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun unsigned int ch = dev->id;
54*4882a593Smuzhiyun tx_7segled_putc(ch, buf[0]);
55*4882a593Smuzhiyun return size;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun static DEVICE_ATTR_WO(ascii);
59*4882a593Smuzhiyun static DEVICE_ATTR_WO(raw);
60*4882a593Smuzhiyun
map_seg7_show(struct device * dev,struct device_attribute * attr,char * buf)61*4882a593Smuzhiyun static ssize_t map_seg7_show(struct device *dev,
62*4882a593Smuzhiyun struct device_attribute *attr,
63*4882a593Smuzhiyun char *buf)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun memcpy(buf, &txx9_seg7map, sizeof(txx9_seg7map));
66*4882a593Smuzhiyun return sizeof(txx9_seg7map);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
map_seg7_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)69*4882a593Smuzhiyun static ssize_t map_seg7_store(struct device *dev,
70*4882a593Smuzhiyun struct device_attribute *attr,
71*4882a593Smuzhiyun const char *buf, size_t size)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun if (size != sizeof(txx9_seg7map))
74*4882a593Smuzhiyun return -EINVAL;
75*4882a593Smuzhiyun memcpy(&txx9_seg7map, buf, size);
76*4882a593Smuzhiyun return size;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun static DEVICE_ATTR(map_seg7, 0600, map_seg7_show, map_seg7_store);
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun static struct bus_type tx_7segled_subsys = {
82*4882a593Smuzhiyun .name = "7segled",
83*4882a593Smuzhiyun .dev_name = "7segled",
84*4882a593Smuzhiyun };
85*4882a593Smuzhiyun
tx_7segled_release(struct device * dev)86*4882a593Smuzhiyun static void tx_7segled_release(struct device *dev)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun kfree(dev);
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
tx_7segled_init_sysfs(void)91*4882a593Smuzhiyun static int __init tx_7segled_init_sysfs(void)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun int error, i;
94*4882a593Smuzhiyun if (!tx_7segled_num)
95*4882a593Smuzhiyun return -ENODEV;
96*4882a593Smuzhiyun error = subsys_system_register(&tx_7segled_subsys, NULL);
97*4882a593Smuzhiyun if (error)
98*4882a593Smuzhiyun return error;
99*4882a593Smuzhiyun error = device_create_file(tx_7segled_subsys.dev_root, &dev_attr_map_seg7);
100*4882a593Smuzhiyun if (error)
101*4882a593Smuzhiyun return error;
102*4882a593Smuzhiyun for (i = 0; i < tx_7segled_num; i++) {
103*4882a593Smuzhiyun struct device *dev;
104*4882a593Smuzhiyun dev = kzalloc(sizeof(*dev), GFP_KERNEL);
105*4882a593Smuzhiyun if (!dev) {
106*4882a593Smuzhiyun error = -ENODEV;
107*4882a593Smuzhiyun break;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun dev->id = i;
110*4882a593Smuzhiyun dev->bus = &tx_7segled_subsys;
111*4882a593Smuzhiyun dev->release = &tx_7segled_release;
112*4882a593Smuzhiyun error = device_register(dev);
113*4882a593Smuzhiyun if (error) {
114*4882a593Smuzhiyun put_device(dev);
115*4882a593Smuzhiyun return error;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun device_create_file(dev, &dev_attr_ascii);
118*4882a593Smuzhiyun device_create_file(dev, &dev_attr_raw);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun return error;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun device_initcall(tx_7segled_init_sysfs);
124