Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ot200_bl.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Bachmann electronic GmbH
3  * Christian Gmeiner <[email protected]>
4  *
5  * Backlight driver for ot200 visualisation device from
6  * Bachmann electronic GmbH.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  */
12 
13 #include <linux/module.h>
14 #include <linux/fb.h>
15 #include <linux/backlight.h>
16 #include <linux/gpio.h>
17 #include <linux/cs5535.h>
18 
19 static struct cs5535_mfgpt_timer *pwm_timer;
20 
21 /* this array defines the mapping of brightness in % to pwm frequency */
22 static const u8 dim_table[101] = {0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
23  2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
24  4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9,
25  10, 10, 11, 11, 12, 12, 13, 14, 15, 15, 16,
26  17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28,
27  30, 31, 33, 35, 37, 39, 41, 43, 45, 47, 50,
28  53, 55, 58, 61, 65, 68, 72, 75, 79, 84, 88,
29  93, 97, 103, 108, 114, 120, 126, 133, 140,
30  147, 155, 163};
31 
34 };
35 
36 #define GPIO_DIMM 27
37 #define SCALE 1
38 #define CMP1MODE 0x2 /* compare on GE; output high on compare
39  * greater than or equal */
40 #define PWM_SETUP (SCALE | CMP1MODE << 6 | MFGPT_SETUP_CNTEN)
41 #define MAX_COMP2 163
42 
43 static int ot200_backlight_update_status(struct backlight_device *bl)
44 {
45  struct ot200_backlight_data *data = bl_get_data(bl);
46  int brightness = bl->props.brightness;
47 
48  if (bl->props.state & BL_CORE_FBBLANK)
49  brightness = 0;
50 
51  /* enable or disable PWM timer */
52  if (brightness == 0)
53  cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP, 0);
54  else if (data->current_brightness == 0) {
58  }
59 
60  /* apply new brightness value */
62  MAX_COMP2 - dim_table[brightness]);
64 
65  return 0;
66 }
67 
68 static int ot200_backlight_get_brightness(struct backlight_device *bl)
69 {
70  struct ot200_backlight_data *data = bl_get_data(bl);
71  return data->current_brightness;
72 }
73 
74 static const struct backlight_ops ot200_backlight_ops = {
75  .update_status = ot200_backlight_update_status,
76  .get_brightness = ot200_backlight_get_brightness,
77 };
78 
79 static int ot200_backlight_probe(struct platform_device *pdev)
80 {
81  struct backlight_device *bl;
82  struct ot200_backlight_data *data;
83  struct backlight_properties props;
84  int retval = 0;
85 
86  /* request gpio */
87  if (devm_gpio_request(&pdev->dev, GPIO_DIMM,
88  "ot200 backlight dimmer") < 0) {
89  dev_err(&pdev->dev, "failed to request GPIO %d\n", GPIO_DIMM);
90  return -ENODEV;
91  }
92 
93  /* request timer */
95  if (!pwm_timer) {
96  dev_err(&pdev->dev, "MFGPT 7 not available\n");
97  return -ENODEV;
98  }
99 
100  data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
101  if (!data) {
102  retval = -ENOMEM;
103  goto error_devm_kzalloc;
104  }
105 
106  /* setup gpio */
109 
110  /* setup timer */
111  cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP1, 0);
114 
115  data->current_brightness = 100;
116  props.max_brightness = 100;
117  props.brightness = 100;
118  props.type = BACKLIGHT_RAW;
119 
120  bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, data,
121  &ot200_backlight_ops, &props);
122  if (IS_ERR(bl)) {
123  dev_err(&pdev->dev, "failed to register backlight\n");
124  retval = PTR_ERR(bl);
125  goto error_devm_kzalloc;
126  }
127 
128  platform_set_drvdata(pdev, bl);
129 
130  return 0;
131 
132 error_devm_kzalloc:
133  cs5535_mfgpt_free_timer(pwm_timer);
134  return retval;
135 }
136 
137 static int ot200_backlight_remove(struct platform_device *pdev)
138 {
139  struct backlight_device *bl = platform_get_drvdata(pdev);
140 
142 
143  /* on module unload set brightness to 100% */
144  cs5535_mfgpt_write(pwm_timer, MFGPT_REG_COUNTER, 0);
147  MAX_COMP2 - dim_table[100]);
148 
149  cs5535_mfgpt_free_timer(pwm_timer);
150 
151  return 0;
152 }
153 
154 static struct platform_driver ot200_backlight_driver = {
155  .driver = {
156  .name = "ot200-backlight",
157  .owner = THIS_MODULE,
158  },
159  .probe = ot200_backlight_probe,
160  .remove = ot200_backlight_remove,
161 };
162 
163 module_platform_driver(ot200_backlight_driver);
164 
165 MODULE_DESCRIPTION("backlight driver for ot200 visualisation device");
166 MODULE_AUTHOR("Christian Gmeiner <[email protected]>");
167 MODULE_LICENSE("GPL");
168 MODULE_ALIAS("platform:ot200-backlight");