Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 1 | /* drivers/video/backlight/platform_lcd.c |
| 2 | * |
| 3 | * Copyright 2008 Simtec Electronics |
| 4 | * Ben Dooks <ben@simtec.co.uk> |
| 5 | * |
| 6 | * Generic platform-device LCD power control interface. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/fb.h> |
| 17 | #include <linux/backlight.h> |
| 18 | #include <linux/lcd.h> |
Jingoo Han | 52e8424 | 2012-10-04 17:13:01 -0700 | [diff] [blame] | 19 | #include <linux/of.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 21 | |
| 22 | #include <video/platform_lcd.h> |
| 23 | |
| 24 | struct platform_lcd { |
| 25 | struct device *us; |
| 26 | struct lcd_device *lcd; |
| 27 | struct plat_lcd_data *pdata; |
| 28 | |
| 29 | unsigned int power; |
Jingoo Han | 8690c70 | 2012-12-17 16:00:31 -0800 | [diff] [blame] | 30 | unsigned int suspended:1; |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | static inline struct platform_lcd *to_our_lcd(struct lcd_device *lcd) |
| 34 | { |
| 35 | return lcd_get_data(lcd); |
| 36 | } |
| 37 | |
| 38 | static int platform_lcd_get_power(struct lcd_device *lcd) |
| 39 | { |
| 40 | struct platform_lcd *plcd = to_our_lcd(lcd); |
| 41 | |
| 42 | return plcd->power; |
| 43 | } |
| 44 | |
| 45 | static int platform_lcd_set_power(struct lcd_device *lcd, int power) |
| 46 | { |
| 47 | struct platform_lcd *plcd = to_our_lcd(lcd); |
| 48 | int lcd_power = 1; |
| 49 | |
| 50 | if (power == FB_BLANK_POWERDOWN || plcd->suspended) |
| 51 | lcd_power = 0; |
| 52 | |
| 53 | plcd->pdata->set_power(plcd->pdata, lcd_power); |
| 54 | plcd->power = power; |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static int platform_lcd_match(struct lcd_device *lcd, struct fb_info *info) |
| 60 | { |
| 61 | struct platform_lcd *plcd = to_our_lcd(lcd); |
| 62 | struct plat_lcd_data *pdata = plcd->pdata; |
| 63 | |
| 64 | if (pdata->match_fb) |
| 65 | return pdata->match_fb(pdata, info); |
| 66 | |
| 67 | return plcd->us->parent == info->device; |
| 68 | } |
| 69 | |
| 70 | static struct lcd_ops platform_lcd_ops = { |
| 71 | .get_power = platform_lcd_get_power, |
| 72 | .set_power = platform_lcd_set_power, |
| 73 | .check_fb = platform_lcd_match, |
| 74 | }; |
| 75 | |
Bill Pemberton | 1b9e450 | 2012-11-19 13:21:46 -0500 | [diff] [blame] | 76 | static int platform_lcd_probe(struct platform_device *pdev) |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 77 | { |
| 78 | struct plat_lcd_data *pdata; |
| 79 | struct platform_lcd *plcd; |
| 80 | struct device *dev = &pdev->dev; |
| 81 | int err; |
| 82 | |
| 83 | pdata = pdev->dev.platform_data; |
| 84 | if (!pdata) { |
| 85 | dev_err(dev, "no platform data supplied\n"); |
| 86 | return -EINVAL; |
| 87 | } |
| 88 | |
Andrew Bresticker | 46e1915 | 2013-04-29 16:17:58 -0700 | [diff] [blame] | 89 | if (pdata->probe) { |
| 90 | err = pdata->probe(pdata); |
| 91 | if (err) |
| 92 | return err; |
| 93 | } |
| 94 | |
Mark Brown | 48e78e8 | 2012-01-10 15:09:21 -0800 | [diff] [blame] | 95 | plcd = devm_kzalloc(&pdev->dev, sizeof(struct platform_lcd), |
| 96 | GFP_KERNEL); |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 97 | if (!plcd) { |
| 98 | dev_err(dev, "no memory for state\n"); |
| 99 | return -ENOMEM; |
| 100 | } |
| 101 | |
| 102 | plcd->us = dev; |
| 103 | plcd->pdata = pdata; |
Ben Dooks | e958d3a | 2008-07-29 22:32:56 -0700 | [diff] [blame] | 104 | plcd->lcd = lcd_device_register(dev_name(dev), dev, |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 105 | plcd, &platform_lcd_ops); |
| 106 | if (IS_ERR(plcd->lcd)) { |
| 107 | dev_err(dev, "cannot register lcd device\n"); |
| 108 | err = PTR_ERR(plcd->lcd); |
Mark Brown | 48e78e8 | 2012-01-10 15:09:21 -0800 | [diff] [blame] | 109 | goto err; |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | platform_set_drvdata(pdev, plcd); |
Ben Dooks | 126ed36 | 2008-07-29 22:33:25 -0700 | [diff] [blame] | 113 | platform_lcd_set_power(plcd->lcd, FB_BLANK_NORMAL); |
| 114 | |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 115 | return 0; |
| 116 | |
Mark Brown | 48e78e8 | 2012-01-10 15:09:21 -0800 | [diff] [blame] | 117 | err: |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 118 | return err; |
| 119 | } |
| 120 | |
Bill Pemberton | 7e4b9d0 | 2012-11-19 13:26:34 -0500 | [diff] [blame] | 121 | static int platform_lcd_remove(struct platform_device *pdev) |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 122 | { |
| 123 | struct platform_lcd *plcd = platform_get_drvdata(pdev); |
| 124 | |
| 125 | lcd_device_unregister(plcd->lcd); |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Jingoo Han | 1d80d0f | 2013-04-29 16:17:26 -0700 | [diff] [blame] | 130 | #ifdef CONFIG_PM_SLEEP |
Jingoo Han | 67a6727 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 131 | static int platform_lcd_suspend(struct device *dev) |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 132 | { |
Jingoo Han | 67a6727 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 133 | struct platform_lcd *plcd = dev_get_drvdata(dev); |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 134 | |
| 135 | plcd->suspended = 1; |
| 136 | platform_lcd_set_power(plcd->lcd, plcd->power); |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Jingoo Han | 67a6727 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 141 | static int platform_lcd_resume(struct device *dev) |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 142 | { |
Jingoo Han | 67a6727 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 143 | struct platform_lcd *plcd = dev_get_drvdata(dev); |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 144 | |
| 145 | plcd->suspended = 0; |
| 146 | platform_lcd_set_power(plcd->lcd, plcd->power); |
| 147 | |
| 148 | return 0; |
| 149 | } |
Jingoo Han | 1d80d0f | 2013-04-29 16:17:26 -0700 | [diff] [blame] | 150 | #endif |
Jingoo Han | 67a6727 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 151 | |
| 152 | static SIMPLE_DEV_PM_OPS(platform_lcd_pm_ops, platform_lcd_suspend, |
| 153 | platform_lcd_resume); |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 154 | |
Jingoo Han | 52e8424 | 2012-10-04 17:13:01 -0700 | [diff] [blame] | 155 | #ifdef CONFIG_OF |
| 156 | static const struct of_device_id platform_lcd_of_match[] = { |
| 157 | { .compatible = "platform-lcd" }, |
| 158 | {}, |
| 159 | }; |
| 160 | MODULE_DEVICE_TABLE(of, platform_lcd_of_match); |
| 161 | #endif |
| 162 | |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 163 | static struct platform_driver platform_lcd_driver = { |
| 164 | .driver = { |
| 165 | .name = "platform-lcd", |
| 166 | .owner = THIS_MODULE, |
Jingoo Han | 67a6727 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 167 | .pm = &platform_lcd_pm_ops, |
Jingoo Han | 52e8424 | 2012-10-04 17:13:01 -0700 | [diff] [blame] | 168 | .of_match_table = of_match_ptr(platform_lcd_of_match), |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 169 | }, |
| 170 | .probe = platform_lcd_probe, |
Bill Pemberton | d1723fa | 2012-11-19 13:21:09 -0500 | [diff] [blame] | 171 | .remove = platform_lcd_remove, |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
Axel Lin | 81178e0 | 2012-01-10 15:09:11 -0800 | [diff] [blame] | 174 | module_platform_driver(platform_lcd_driver); |
Ben Dooks | c25826a | 2008-07-23 21:31:38 -0700 | [diff] [blame] | 175 | |
| 176 | MODULE_AUTHOR("Ben Dooks <ben-linux@fluff.org>"); |
| 177 | MODULE_LICENSE("GPL v2"); |
| 178 | MODULE_ALIAS("platform:platform-lcd"); |