Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
da903x_bl.c
Go to the documentation of this file.
1 /*
2  * Backlight driver for Dialog Semiconductor DA9030/DA9034
3  *
4  * Copyright (C) 2008 Compulab, Ltd.
5  * Mike Rapoport <[email protected]>
6  *
7  * Copyright (C) 2006-2008 Marvell International Ltd.
8  * Eric Miao <[email protected]>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14 
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/platform_device.h>
18 #include <linux/fb.h>
19 #include <linux/backlight.h>
20 #include <linux/mfd/da903x.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 
24 #define DA9030_WLED_CONTROL 0x25
25 #define DA9030_WLED_CP_EN (1 << 6)
26 #define DA9030_WLED_TRIM(x) ((x) & 0x7)
27 
28 #define DA9034_WLED_CONTROL1 0x3C
29 #define DA9034_WLED_CONTROL2 0x3D
30 #define DA9034_WLED_ISET(x) ((x) & 0x1f)
31 
32 #define DA9034_WLED_BOOST_EN (1 << 5)
33 
34 #define DA9030_MAX_BRIGHTNESS 7
35 #define DA9034_MAX_BRIGHTNESS 0x7f
36 
38  struct device *da903x_dev;
39  int id;
41 };
42 
43 static int da903x_backlight_set(struct backlight_device *bl, int brightness)
44 {
45  struct da903x_backlight_data *data = bl_get_data(bl);
46  struct device *dev = data->da903x_dev;
47  uint8_t val;
48  int ret = 0;
49 
50  switch (data->id) {
51  case DA9034_ID_WLED:
53  brightness, 0x7f);
54  if (ret)
55  return ret;
56 
57  if (data->current_brightness && brightness == 0)
58  ret = da903x_clr_bits(dev,
61 
62  if (data->current_brightness == 0 && brightness)
63  ret = da903x_set_bits(dev,
66  break;
67  case DA9030_ID_WLED:
68  val = DA9030_WLED_TRIM(brightness);
69  val |= brightness ? DA9030_WLED_CP_EN : 0;
70  ret = da903x_write(dev, DA9030_WLED_CONTROL, val);
71  break;
72  }
73 
74  if (ret)
75  return ret;
76 
78  return 0;
79 }
80 
81 static int da903x_backlight_update_status(struct backlight_device *bl)
82 {
83  int brightness = bl->props.brightness;
84 
85  if (bl->props.power != FB_BLANK_UNBLANK)
86  brightness = 0;
87 
88  if (bl->props.fb_blank != FB_BLANK_UNBLANK)
89  brightness = 0;
90 
91  return da903x_backlight_set(bl, brightness);
92 }
93 
94 static int da903x_backlight_get_brightness(struct backlight_device *bl)
95 {
96  struct da903x_backlight_data *data = bl_get_data(bl);
97  return data->current_brightness;
98 }
99 
100 static const struct backlight_ops da903x_backlight_ops = {
101  .update_status = da903x_backlight_update_status,
102  .get_brightness = da903x_backlight_get_brightness,
103 };
104 
105 static int da903x_backlight_probe(struct platform_device *pdev)
106 {
107  struct da9034_backlight_pdata *pdata = pdev->dev.platform_data;
108  struct da903x_backlight_data *data;
109  struct backlight_device *bl;
110  struct backlight_properties props;
111  int max_brightness;
112 
113  data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
114  if (data == NULL)
115  return -ENOMEM;
116 
117  switch (pdev->id) {
118  case DA9030_ID_WLED:
119  max_brightness = DA9030_MAX_BRIGHTNESS;
120  break;
121  case DA9034_ID_WLED:
122  max_brightness = DA9034_MAX_BRIGHTNESS;
123  break;
124  default:
125  dev_err(&pdev->dev, "invalid backlight device ID(%d)\n",
126  pdev->id);
127  return -EINVAL;
128  }
129 
130  data->id = pdev->id;
131  data->da903x_dev = pdev->dev.parent;
132  data->current_brightness = 0;
133 
134  /* adjust the WLED output current */
135  if (pdata)
138 
139  memset(&props, 0, sizeof(props));
140  props.type = BACKLIGHT_RAW;
141  props.max_brightness = max_brightness;
142  bl = backlight_device_register(pdev->name, data->da903x_dev, data,
143  &da903x_backlight_ops, &props);
144  if (IS_ERR(bl)) {
145  dev_err(&pdev->dev, "failed to register backlight\n");
146  return PTR_ERR(bl);
147  }
148 
149  bl->props.brightness = max_brightness;
150 
151  platform_set_drvdata(pdev, bl);
152  backlight_update_status(bl);
153  return 0;
154 }
155 
156 static int da903x_backlight_remove(struct platform_device *pdev)
157 {
158  struct backlight_device *bl = platform_get_drvdata(pdev);
159 
161  return 0;
162 }
163 
164 #ifdef CONFIG_PM
165 static int da903x_backlight_suspend(struct device *dev)
166 {
167  struct platform_device *pdev = to_platform_device(dev);
168  struct backlight_device *bl = platform_get_drvdata(pdev);
169  return da903x_backlight_set(bl, 0);
170 }
171 
172 static int da903x_backlight_resume(struct device *dev)
173 {
174  struct platform_device *pdev = to_platform_device(dev);
175  struct backlight_device *bl = platform_get_drvdata(pdev);
176 
177  backlight_update_status(bl);
178  return 0;
179 }
180 
181 static const struct dev_pm_ops da903x_backlight_pm_ops = {
182  .suspend = da903x_backlight_suspend,
183  .resume = da903x_backlight_resume,
184 };
185 #endif
186 
187 static struct platform_driver da903x_backlight_driver = {
188  .driver = {
189  .name = "da903x-backlight",
190  .owner = THIS_MODULE,
191 #ifdef CONFIG_PM
192  .pm = &da903x_backlight_pm_ops,
193 #endif
194  },
195  .probe = da903x_backlight_probe,
196  .remove = da903x_backlight_remove,
197 };
198 
199 module_platform_driver(da903x_backlight_driver);
200 
201 MODULE_DESCRIPTION("Backlight Driver for Dialog Semiconductor DA9030/DA9034");
202 MODULE_AUTHOR("Eric Miao <[email protected]>"
203  "Mike Rapoport <[email protected]>");
204 MODULE_LICENSE("GPL");
205 MODULE_ALIAS("platform:da903x-backlight");