intel_oaktrail.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * intel_oaktrail.c - Intel OakTrail Platform support.
  3. *
  4. * Copyright (C) 2010-2011 Intel Corporation
  5. * Author: Yin Kangkai (kangkai.yin@intel.com)
  6. *
  7. * based on Compal driver, Copyright (C) 2008 Cezary Jackiewicz
  8. * <cezary.jackiewicz (at) gmail.com>, based on MSI driver
  9. * Copyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  24. * 02110-1301, USA.
  25. *
  26. * This driver does below things:
  27. * 1. registers itself in the Linux backlight control in
  28. * /sys/class/backlight/intel_oaktrail/
  29. *
  30. * 2. registers in the rfkill subsystem here: /sys/class/rfkill/rfkillX/
  31. * for these components: wifi, bluetooth, wwan (3g), gps
  32. *
  33. * This driver might work on other products based on Oaktrail. If you
  34. * want to try it you can pass force=1 as argument to the module which
  35. * will force it to load even when the DMI data doesn't identify the
  36. * product as compatible.
  37. */
  38. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  39. #include <linux/module.h>
  40. #include <linux/kernel.h>
  41. #include <linux/init.h>
  42. #include <linux/acpi.h>
  43. #include <linux/fb.h>
  44. #include <linux/mutex.h>
  45. #include <linux/err.h>
  46. #include <linux/i2c.h>
  47. #include <linux/backlight.h>
  48. #include <linux/platform_device.h>
  49. #include <linux/dmi.h>
  50. #include <linux/rfkill.h>
  51. #include <acpi/acpi_bus.h>
  52. #include <acpi/acpi_drivers.h>
  53. #define DRIVER_NAME "intel_oaktrail"
  54. #define DRIVER_VERSION "0.4ac1"
  55. /*
  56. * This is the devices status address in EC space, and the control bits
  57. * definition:
  58. *
  59. * (1 << 0): Camera enable/disable, RW.
  60. * (1 << 1): Bluetooth enable/disable, RW.
  61. * (1 << 2): GPS enable/disable, RW.
  62. * (1 << 3): WiFi enable/disable, RW.
  63. * (1 << 4): WWAN (3G) enable/disalbe, RW.
  64. * (1 << 5): Touchscreen enable/disable, Read Only.
  65. */
  66. #define OT_EC_DEVICE_STATE_ADDRESS 0xD6
  67. #define OT_EC_CAMERA_MASK (1 << 0)
  68. #define OT_EC_BT_MASK (1 << 1)
  69. #define OT_EC_GPS_MASK (1 << 2)
  70. #define OT_EC_WIFI_MASK (1 << 3)
  71. #define OT_EC_WWAN_MASK (1 << 4)
  72. #define OT_EC_TS_MASK (1 << 5)
  73. /*
  74. * This is the address in EC space and commands used to control LCD backlight:
  75. *
  76. * Two steps needed to change the LCD backlight:
  77. * 1. write the backlight percentage into OT_EC_BL_BRIGHTNESS_ADDRESS;
  78. * 2. write OT_EC_BL_CONTROL_ON_DATA into OT_EC_BL_CONTROL_ADDRESS.
  79. *
  80. * To read the LCD back light, just read out the value from
  81. * OT_EC_BL_BRIGHTNESS_ADDRESS.
  82. *
  83. * LCD backlight brightness range: 0 - 100 (OT_EC_BL_BRIGHTNESS_MAX)
  84. */
  85. #define OT_EC_BL_BRIGHTNESS_ADDRESS 0x44
  86. #define OT_EC_BL_BRIGHTNESS_MAX 100
  87. #define OT_EC_BL_CONTROL_ADDRESS 0x3A
  88. #define OT_EC_BL_CONTROL_ON_DATA 0x1A
  89. static bool force;
  90. module_param(force, bool, 0);
  91. MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
  92. static struct platform_device *oaktrail_device;
  93. static struct backlight_device *oaktrail_bl_device;
  94. static struct rfkill *bt_rfkill;
  95. static struct rfkill *gps_rfkill;
  96. static struct rfkill *wifi_rfkill;
  97. static struct rfkill *wwan_rfkill;
  98. /* rfkill */
  99. static int oaktrail_rfkill_set(void *data, bool blocked)
  100. {
  101. u8 value;
  102. u8 result;
  103. unsigned long radio = (unsigned long) data;
  104. ec_read(OT_EC_DEVICE_STATE_ADDRESS, &result);
  105. if (!blocked)
  106. value = (u8) (result | radio);
  107. else
  108. value = (u8) (result & ~radio);
  109. ec_write(OT_EC_DEVICE_STATE_ADDRESS, value);
  110. return 0;
  111. }
  112. static const struct rfkill_ops oaktrail_rfkill_ops = {
  113. .set_block = oaktrail_rfkill_set,
  114. };
  115. static struct rfkill *oaktrail_rfkill_new(char *name, enum rfkill_type type,
  116. unsigned long mask)
  117. {
  118. struct rfkill *rfkill_dev;
  119. u8 value;
  120. int err;
  121. rfkill_dev = rfkill_alloc(name, &oaktrail_device->dev, type,
  122. &oaktrail_rfkill_ops, (void *)mask);
  123. if (!rfkill_dev)
  124. return ERR_PTR(-ENOMEM);
  125. ec_read(OT_EC_DEVICE_STATE_ADDRESS, &value);
  126. rfkill_init_sw_state(rfkill_dev, (value & mask) != 1);
  127. err = rfkill_register(rfkill_dev);
  128. if (err) {
  129. rfkill_destroy(rfkill_dev);
  130. return ERR_PTR(err);
  131. }
  132. return rfkill_dev;
  133. }
  134. static inline void __oaktrail_rfkill_cleanup(struct rfkill *rf)
  135. {
  136. if (rf) {
  137. rfkill_unregister(rf);
  138. rfkill_destroy(rf);
  139. }
  140. }
  141. static void oaktrail_rfkill_cleanup(void)
  142. {
  143. __oaktrail_rfkill_cleanup(wifi_rfkill);
  144. __oaktrail_rfkill_cleanup(bt_rfkill);
  145. __oaktrail_rfkill_cleanup(gps_rfkill);
  146. __oaktrail_rfkill_cleanup(wwan_rfkill);
  147. }
  148. static int oaktrail_rfkill_init(void)
  149. {
  150. int ret;
  151. wifi_rfkill = oaktrail_rfkill_new("oaktrail-wifi",
  152. RFKILL_TYPE_WLAN,
  153. OT_EC_WIFI_MASK);
  154. if (IS_ERR(wifi_rfkill)) {
  155. ret = PTR_ERR(wifi_rfkill);
  156. wifi_rfkill = NULL;
  157. goto cleanup;
  158. }
  159. bt_rfkill = oaktrail_rfkill_new("oaktrail-bluetooth",
  160. RFKILL_TYPE_BLUETOOTH,
  161. OT_EC_BT_MASK);
  162. if (IS_ERR(bt_rfkill)) {
  163. ret = PTR_ERR(bt_rfkill);
  164. bt_rfkill = NULL;
  165. goto cleanup;
  166. }
  167. gps_rfkill = oaktrail_rfkill_new("oaktrail-gps",
  168. RFKILL_TYPE_GPS,
  169. OT_EC_GPS_MASK);
  170. if (IS_ERR(gps_rfkill)) {
  171. ret = PTR_ERR(gps_rfkill);
  172. gps_rfkill = NULL;
  173. goto cleanup;
  174. }
  175. wwan_rfkill = oaktrail_rfkill_new("oaktrail-wwan",
  176. RFKILL_TYPE_WWAN,
  177. OT_EC_WWAN_MASK);
  178. if (IS_ERR(wwan_rfkill)) {
  179. ret = PTR_ERR(wwan_rfkill);
  180. wwan_rfkill = NULL;
  181. goto cleanup;
  182. }
  183. return 0;
  184. cleanup:
  185. oaktrail_rfkill_cleanup();
  186. return ret;
  187. }
  188. /* backlight */
  189. static int get_backlight_brightness(struct backlight_device *b)
  190. {
  191. u8 value;
  192. ec_read(OT_EC_BL_BRIGHTNESS_ADDRESS, &value);
  193. return value;
  194. }
  195. static int set_backlight_brightness(struct backlight_device *b)
  196. {
  197. u8 percent = (u8) b->props.brightness;
  198. if (percent < 0 || percent > OT_EC_BL_BRIGHTNESS_MAX)
  199. return -EINVAL;
  200. ec_write(OT_EC_BL_BRIGHTNESS_ADDRESS, percent);
  201. ec_write(OT_EC_BL_CONTROL_ADDRESS, OT_EC_BL_CONTROL_ON_DATA);
  202. return 0;
  203. }
  204. static const struct backlight_ops oaktrail_bl_ops = {
  205. .get_brightness = get_backlight_brightness,
  206. .update_status = set_backlight_brightness,
  207. };
  208. static int oaktrail_backlight_init(void)
  209. {
  210. struct backlight_device *bd;
  211. struct backlight_properties props;
  212. memset(&props, 0, sizeof(struct backlight_properties));
  213. props.type = BACKLIGHT_PLATFORM;
  214. props.max_brightness = OT_EC_BL_BRIGHTNESS_MAX;
  215. bd = backlight_device_register(DRIVER_NAME,
  216. &oaktrail_device->dev, NULL,
  217. &oaktrail_bl_ops,
  218. &props);
  219. if (IS_ERR(bd)) {
  220. oaktrail_bl_device = NULL;
  221. pr_warning("Unable to register backlight device\n");
  222. return PTR_ERR(bd);
  223. }
  224. oaktrail_bl_device = bd;
  225. bd->props.brightness = get_backlight_brightness(bd);
  226. bd->props.power = FB_BLANK_UNBLANK;
  227. backlight_update_status(bd);
  228. return 0;
  229. }
  230. static void oaktrail_backlight_exit(void)
  231. {
  232. if (oaktrail_bl_device)
  233. backlight_device_unregister(oaktrail_bl_device);
  234. }
  235. static int __devinit oaktrail_probe(struct platform_device *pdev)
  236. {
  237. return 0;
  238. }
  239. static int __devexit oaktrail_remove(struct platform_device *pdev)
  240. {
  241. return 0;
  242. }
  243. static struct platform_driver oaktrail_driver = {
  244. .driver = {
  245. .name = DRIVER_NAME,
  246. .owner = THIS_MODULE,
  247. },
  248. .probe = oaktrail_probe,
  249. .remove = __devexit_p(oaktrail_remove)
  250. };
  251. static int dmi_check_cb(const struct dmi_system_id *id)
  252. {
  253. pr_info("Identified model '%s'\n", id->ident);
  254. return 0;
  255. }
  256. static struct dmi_system_id __initdata oaktrail_dmi_table[] = {
  257. {
  258. .ident = "OakTrail platform",
  259. .matches = {
  260. DMI_MATCH(DMI_PRODUCT_NAME, "OakTrail platform"),
  261. },
  262. .callback = dmi_check_cb
  263. },
  264. { }
  265. };
  266. MODULE_DEVICE_TABLE(dmi, oaktrail_dmi_table);
  267. static int __init oaktrail_init(void)
  268. {
  269. int ret;
  270. if (acpi_disabled) {
  271. pr_err("ACPI needs to be enabled for this driver to work!\n");
  272. return -ENODEV;
  273. }
  274. if (!force && !dmi_check_system(oaktrail_dmi_table)) {
  275. pr_err("Platform not recognized (You could try the module's force-parameter)");
  276. return -ENODEV;
  277. }
  278. ret = platform_driver_register(&oaktrail_driver);
  279. if (ret) {
  280. pr_warning("Unable to register platform driver\n");
  281. goto err_driver_reg;
  282. }
  283. oaktrail_device = platform_device_alloc(DRIVER_NAME, -1);
  284. if (!oaktrail_device) {
  285. pr_warning("Unable to allocate platform device\n");
  286. ret = -ENOMEM;
  287. goto err_device_alloc;
  288. }
  289. ret = platform_device_add(oaktrail_device);
  290. if (ret) {
  291. pr_warning("Unable to add platform device\n");
  292. goto err_device_add;
  293. }
  294. if (!acpi_video_backlight_support()) {
  295. ret = oaktrail_backlight_init();
  296. if (ret)
  297. goto err_backlight;
  298. } else
  299. pr_info("Backlight controlled by ACPI video driver\n");
  300. ret = oaktrail_rfkill_init();
  301. if (ret) {
  302. pr_warning("Setup rfkill failed\n");
  303. goto err_rfkill;
  304. }
  305. pr_info("Driver "DRIVER_VERSION" successfully loaded\n");
  306. return 0;
  307. err_rfkill:
  308. oaktrail_backlight_exit();
  309. err_backlight:
  310. platform_device_del(oaktrail_device);
  311. err_device_add:
  312. platform_device_put(oaktrail_device);
  313. err_device_alloc:
  314. platform_driver_unregister(&oaktrail_driver);
  315. err_driver_reg:
  316. return ret;
  317. }
  318. static void __exit oaktrail_cleanup(void)
  319. {
  320. oaktrail_backlight_exit();
  321. oaktrail_rfkill_cleanup();
  322. platform_device_unregister(oaktrail_device);
  323. platform_driver_unregister(&oaktrail_driver);
  324. pr_info("Driver unloaded\n");
  325. }
  326. module_init(oaktrail_init);
  327. module_exit(oaktrail_cleanup);
  328. MODULE_AUTHOR("Yin Kangkai (kangkai.yin@intel.com)");
  329. MODULE_DESCRIPTION("Intel Oaktrail Platform ACPI Extras");
  330. MODULE_VERSION(DRIVER_VERSION);
  331. MODULE_LICENSE("GPL");