intel_oaktrail.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. #define DRIVER_NAME "intel_oaktrail"
  52. #define DRIVER_VERSION "0.4ac1"
  53. /*
  54. * This is the devices status address in EC space, and the control bits
  55. * definition:
  56. *
  57. * (1 << 0): Camera enable/disable, RW.
  58. * (1 << 1): Bluetooth enable/disable, RW.
  59. * (1 << 2): GPS enable/disable, RW.
  60. * (1 << 3): WiFi enable/disable, RW.
  61. * (1 << 4): WWAN (3G) enable/disalbe, RW.
  62. * (1 << 5): Touchscreen enable/disable, Read Only.
  63. */
  64. #define OT_EC_DEVICE_STATE_ADDRESS 0xD6
  65. #define OT_EC_CAMERA_MASK (1 << 0)
  66. #define OT_EC_BT_MASK (1 << 1)
  67. #define OT_EC_GPS_MASK (1 << 2)
  68. #define OT_EC_WIFI_MASK (1 << 3)
  69. #define OT_EC_WWAN_MASK (1 << 4)
  70. #define OT_EC_TS_MASK (1 << 5)
  71. /*
  72. * This is the address in EC space and commands used to control LCD backlight:
  73. *
  74. * Two steps needed to change the LCD backlight:
  75. * 1. write the backlight percentage into OT_EC_BL_BRIGHTNESS_ADDRESS;
  76. * 2. write OT_EC_BL_CONTROL_ON_DATA into OT_EC_BL_CONTROL_ADDRESS.
  77. *
  78. * To read the LCD back light, just read out the value from
  79. * OT_EC_BL_BRIGHTNESS_ADDRESS.
  80. *
  81. * LCD backlight brightness range: 0 - 100 (OT_EC_BL_BRIGHTNESS_MAX)
  82. */
  83. #define OT_EC_BL_BRIGHTNESS_ADDRESS 0x44
  84. #define OT_EC_BL_BRIGHTNESS_MAX 100
  85. #define OT_EC_BL_CONTROL_ADDRESS 0x3A
  86. #define OT_EC_BL_CONTROL_ON_DATA 0x1A
  87. static bool force;
  88. module_param(force, bool, 0);
  89. MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
  90. static struct platform_device *oaktrail_device;
  91. static struct backlight_device *oaktrail_bl_device;
  92. static struct rfkill *bt_rfkill;
  93. static struct rfkill *gps_rfkill;
  94. static struct rfkill *wifi_rfkill;
  95. static struct rfkill *wwan_rfkill;
  96. /* rfkill */
  97. static int oaktrail_rfkill_set(void *data, bool blocked)
  98. {
  99. u8 value;
  100. u8 result;
  101. unsigned long radio = (unsigned long) data;
  102. ec_read(OT_EC_DEVICE_STATE_ADDRESS, &result);
  103. if (!blocked)
  104. value = (u8) (result | radio);
  105. else
  106. value = (u8) (result & ~radio);
  107. ec_write(OT_EC_DEVICE_STATE_ADDRESS, value);
  108. return 0;
  109. }
  110. static const struct rfkill_ops oaktrail_rfkill_ops = {
  111. .set_block = oaktrail_rfkill_set,
  112. };
  113. static struct rfkill *oaktrail_rfkill_new(char *name, enum rfkill_type type,
  114. unsigned long mask)
  115. {
  116. struct rfkill *rfkill_dev;
  117. u8 value;
  118. int err;
  119. rfkill_dev = rfkill_alloc(name, &oaktrail_device->dev, type,
  120. &oaktrail_rfkill_ops, (void *)mask);
  121. if (!rfkill_dev)
  122. return ERR_PTR(-ENOMEM);
  123. ec_read(OT_EC_DEVICE_STATE_ADDRESS, &value);
  124. rfkill_init_sw_state(rfkill_dev, (value & mask) != 1);
  125. err = rfkill_register(rfkill_dev);
  126. if (err) {
  127. rfkill_destroy(rfkill_dev);
  128. return ERR_PTR(err);
  129. }
  130. return rfkill_dev;
  131. }
  132. static inline void __oaktrail_rfkill_cleanup(struct rfkill *rf)
  133. {
  134. if (rf) {
  135. rfkill_unregister(rf);
  136. rfkill_destroy(rf);
  137. }
  138. }
  139. static void oaktrail_rfkill_cleanup(void)
  140. {
  141. __oaktrail_rfkill_cleanup(wifi_rfkill);
  142. __oaktrail_rfkill_cleanup(bt_rfkill);
  143. __oaktrail_rfkill_cleanup(gps_rfkill);
  144. __oaktrail_rfkill_cleanup(wwan_rfkill);
  145. }
  146. static int oaktrail_rfkill_init(void)
  147. {
  148. int ret;
  149. wifi_rfkill = oaktrail_rfkill_new("oaktrail-wifi",
  150. RFKILL_TYPE_WLAN,
  151. OT_EC_WIFI_MASK);
  152. if (IS_ERR(wifi_rfkill)) {
  153. ret = PTR_ERR(wifi_rfkill);
  154. wifi_rfkill = NULL;
  155. goto cleanup;
  156. }
  157. bt_rfkill = oaktrail_rfkill_new("oaktrail-bluetooth",
  158. RFKILL_TYPE_BLUETOOTH,
  159. OT_EC_BT_MASK);
  160. if (IS_ERR(bt_rfkill)) {
  161. ret = PTR_ERR(bt_rfkill);
  162. bt_rfkill = NULL;
  163. goto cleanup;
  164. }
  165. gps_rfkill = oaktrail_rfkill_new("oaktrail-gps",
  166. RFKILL_TYPE_GPS,
  167. OT_EC_GPS_MASK);
  168. if (IS_ERR(gps_rfkill)) {
  169. ret = PTR_ERR(gps_rfkill);
  170. gps_rfkill = NULL;
  171. goto cleanup;
  172. }
  173. wwan_rfkill = oaktrail_rfkill_new("oaktrail-wwan",
  174. RFKILL_TYPE_WWAN,
  175. OT_EC_WWAN_MASK);
  176. if (IS_ERR(wwan_rfkill)) {
  177. ret = PTR_ERR(wwan_rfkill);
  178. wwan_rfkill = NULL;
  179. goto cleanup;
  180. }
  181. return 0;
  182. cleanup:
  183. oaktrail_rfkill_cleanup();
  184. return ret;
  185. }
  186. /* backlight */
  187. static int get_backlight_brightness(struct backlight_device *b)
  188. {
  189. u8 value;
  190. ec_read(OT_EC_BL_BRIGHTNESS_ADDRESS, &value);
  191. return value;
  192. }
  193. static int set_backlight_brightness(struct backlight_device *b)
  194. {
  195. u8 percent = (u8) b->props.brightness;
  196. if (percent < 0 || percent > OT_EC_BL_BRIGHTNESS_MAX)
  197. return -EINVAL;
  198. ec_write(OT_EC_BL_BRIGHTNESS_ADDRESS, percent);
  199. ec_write(OT_EC_BL_CONTROL_ADDRESS, OT_EC_BL_CONTROL_ON_DATA);
  200. return 0;
  201. }
  202. static const struct backlight_ops oaktrail_bl_ops = {
  203. .get_brightness = get_backlight_brightness,
  204. .update_status = set_backlight_brightness,
  205. };
  206. static int oaktrail_backlight_init(void)
  207. {
  208. struct backlight_device *bd;
  209. struct backlight_properties props;
  210. memset(&props, 0, sizeof(struct backlight_properties));
  211. props.type = BACKLIGHT_PLATFORM;
  212. props.max_brightness = OT_EC_BL_BRIGHTNESS_MAX;
  213. bd = backlight_device_register(DRIVER_NAME,
  214. &oaktrail_device->dev, NULL,
  215. &oaktrail_bl_ops,
  216. &props);
  217. if (IS_ERR(bd)) {
  218. oaktrail_bl_device = NULL;
  219. pr_warning("Unable to register backlight device\n");
  220. return PTR_ERR(bd);
  221. }
  222. oaktrail_bl_device = bd;
  223. bd->props.brightness = get_backlight_brightness(bd);
  224. bd->props.power = FB_BLANK_UNBLANK;
  225. backlight_update_status(bd);
  226. return 0;
  227. }
  228. static void oaktrail_backlight_exit(void)
  229. {
  230. if (oaktrail_bl_device)
  231. backlight_device_unregister(oaktrail_bl_device);
  232. }
  233. static int oaktrail_probe(struct platform_device *pdev)
  234. {
  235. return 0;
  236. }
  237. static int oaktrail_remove(struct platform_device *pdev)
  238. {
  239. return 0;
  240. }
  241. static struct platform_driver oaktrail_driver = {
  242. .driver = {
  243. .name = DRIVER_NAME,
  244. .owner = THIS_MODULE,
  245. },
  246. .probe = oaktrail_probe,
  247. .remove = oaktrail_remove,
  248. };
  249. static int dmi_check_cb(const struct dmi_system_id *id)
  250. {
  251. pr_info("Identified model '%s'\n", id->ident);
  252. return 0;
  253. }
  254. static struct dmi_system_id __initdata oaktrail_dmi_table[] = {
  255. {
  256. .ident = "OakTrail platform",
  257. .matches = {
  258. DMI_MATCH(DMI_PRODUCT_NAME, "OakTrail platform"),
  259. },
  260. .callback = dmi_check_cb
  261. },
  262. { }
  263. };
  264. MODULE_DEVICE_TABLE(dmi, oaktrail_dmi_table);
  265. static int __init oaktrail_init(void)
  266. {
  267. int ret;
  268. if (acpi_disabled) {
  269. pr_err("ACPI needs to be enabled for this driver to work!\n");
  270. return -ENODEV;
  271. }
  272. if (!force && !dmi_check_system(oaktrail_dmi_table)) {
  273. pr_err("Platform not recognized (You could try the module's force-parameter)");
  274. return -ENODEV;
  275. }
  276. ret = platform_driver_register(&oaktrail_driver);
  277. if (ret) {
  278. pr_warning("Unable to register platform driver\n");
  279. goto err_driver_reg;
  280. }
  281. oaktrail_device = platform_device_alloc(DRIVER_NAME, -1);
  282. if (!oaktrail_device) {
  283. pr_warning("Unable to allocate platform device\n");
  284. ret = -ENOMEM;
  285. goto err_device_alloc;
  286. }
  287. ret = platform_device_add(oaktrail_device);
  288. if (ret) {
  289. pr_warning("Unable to add platform device\n");
  290. goto err_device_add;
  291. }
  292. if (!acpi_video_backlight_support()) {
  293. ret = oaktrail_backlight_init();
  294. if (ret)
  295. goto err_backlight;
  296. } else
  297. pr_info("Backlight controlled by ACPI video driver\n");
  298. ret = oaktrail_rfkill_init();
  299. if (ret) {
  300. pr_warning("Setup rfkill failed\n");
  301. goto err_rfkill;
  302. }
  303. pr_info("Driver "DRIVER_VERSION" successfully loaded\n");
  304. return 0;
  305. err_rfkill:
  306. oaktrail_backlight_exit();
  307. err_backlight:
  308. platform_device_del(oaktrail_device);
  309. err_device_add:
  310. platform_device_put(oaktrail_device);
  311. err_device_alloc:
  312. platform_driver_unregister(&oaktrail_driver);
  313. err_driver_reg:
  314. return ret;
  315. }
  316. static void __exit oaktrail_cleanup(void)
  317. {
  318. oaktrail_backlight_exit();
  319. oaktrail_rfkill_cleanup();
  320. platform_device_unregister(oaktrail_device);
  321. platform_driver_unregister(&oaktrail_driver);
  322. pr_info("Driver unloaded\n");
  323. }
  324. module_init(oaktrail_init);
  325. module_exit(oaktrail_cleanup);
  326. MODULE_AUTHOR("Yin Kangkai (kangkai.yin@intel.com)");
  327. MODULE_DESCRIPTION("Intel Oaktrail Platform ACPI Extras");
  328. MODULE_VERSION(DRIVER_VERSION);
  329. MODULE_LICENSE("GPL");