1
0

oaktrail.c 8.6 KB

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