leds-ss4200.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * SS4200-E Hardware API
  3. * Copyright (c) 2009, Intel Corporation.
  4. * Copyright IBM Corporation, 2009
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Author: Dave Hansen <dave@sr71.net>
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/dmi.h>
  23. #include <linux/init.h>
  24. #include <linux/ioport.h>
  25. #include <linux/kernel.h>
  26. #include <linux/leds.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/types.h>
  30. #include <linux/uaccess.h>
  31. MODULE_AUTHOR("Rodney Girod <rgirod@confocus.com>, Dave Hansen <dave@sr71.net>");
  32. MODULE_DESCRIPTION("Intel NAS/Home Server ICH7 GPIO Driver");
  33. MODULE_LICENSE("GPL");
  34. /*
  35. * ICH7 LPC/GPIO PCI Config register offsets
  36. */
  37. #define PMBASE 0x040
  38. #define GPIO_BASE 0x048
  39. #define GPIO_CTRL 0x04c
  40. #define GPIO_EN 0x010
  41. /*
  42. * The ICH7 GPIO register block is 64 bytes in size.
  43. */
  44. #define ICH7_GPIO_SIZE 64
  45. /*
  46. * Define register offsets within the ICH7 register block.
  47. */
  48. #define GPIO_USE_SEL 0x000
  49. #define GP_IO_SEL 0x004
  50. #define GP_LVL 0x00c
  51. #define GPO_BLINK 0x018
  52. #define GPI_INV 0x030
  53. #define GPIO_USE_SEL2 0x034
  54. #define GP_IO_SEL2 0x038
  55. #define GP_LVL2 0x03c
  56. /*
  57. * PCI ID of the Intel ICH7 LPC Device within which the GPIO block lives.
  58. */
  59. static const struct pci_device_id ich7_lpc_pci_id[] = {
  60. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_0) },
  61. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_1) },
  62. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_30) },
  63. { } /* NULL entry */
  64. };
  65. MODULE_DEVICE_TABLE(pci, ich7_lpc_pci_id);
  66. static int __init ss4200_led_dmi_callback(const struct dmi_system_id *id)
  67. {
  68. pr_info("detected '%s'\n", id->ident);
  69. return 1;
  70. }
  71. static bool nodetect;
  72. module_param_named(nodetect, nodetect, bool, 0);
  73. MODULE_PARM_DESC(nodetect, "Skip DMI-based hardware detection");
  74. /*
  75. * struct nas_led_whitelist - List of known good models
  76. *
  77. * Contains the known good models this driver is compatible with.
  78. * When adding a new model try to be as strict as possible. This
  79. * makes it possible to keep the false positives (the model is
  80. * detected as working, but in reality it is not) as low as
  81. * possible.
  82. */
  83. static struct dmi_system_id nas_led_whitelist[] __initdata = {
  84. {
  85. .callback = ss4200_led_dmi_callback,
  86. .ident = "Intel SS4200-E",
  87. .matches = {
  88. DMI_MATCH(DMI_SYS_VENDOR, "Intel"),
  89. DMI_MATCH(DMI_PRODUCT_NAME, "SS4200-E"),
  90. DMI_MATCH(DMI_PRODUCT_VERSION, "1.00.00")
  91. }
  92. },
  93. {}
  94. };
  95. /*
  96. * Base I/O address assigned to the Power Management register block
  97. */
  98. static u32 g_pm_io_base;
  99. /*
  100. * Base I/O address assigned to the ICH7 GPIO register block
  101. */
  102. static u32 nas_gpio_io_base;
  103. /*
  104. * When we successfully register a region, we are returned a resource.
  105. * We use these to identify which regions we need to release on our way
  106. * back out.
  107. */
  108. static struct resource *gp_gpio_resource;
  109. struct nasgpio_led {
  110. char *name;
  111. u32 gpio_bit;
  112. struct led_classdev led_cdev;
  113. };
  114. /*
  115. * gpio_bit(s) are the ICH7 GPIO bit assignments
  116. */
  117. static struct nasgpio_led nasgpio_leds[] = {
  118. { .name = "hdd1:blue:sata", .gpio_bit = 0 },
  119. { .name = "hdd1:amber:sata", .gpio_bit = 1 },
  120. { .name = "hdd2:blue:sata", .gpio_bit = 2 },
  121. { .name = "hdd2:amber:sata", .gpio_bit = 3 },
  122. { .name = "hdd3:blue:sata", .gpio_bit = 4 },
  123. { .name = "hdd3:amber:sata", .gpio_bit = 5 },
  124. { .name = "hdd4:blue:sata", .gpio_bit = 6 },
  125. { .name = "hdd4:amber:sata", .gpio_bit = 7 },
  126. { .name = "power:blue:power", .gpio_bit = 27},
  127. { .name = "power:amber:power", .gpio_bit = 28},
  128. };
  129. #define NAS_RECOVERY 0x00000400 /* GPIO10 */
  130. static struct nasgpio_led *
  131. led_classdev_to_nasgpio_led(struct led_classdev *led_cdev)
  132. {
  133. return container_of(led_cdev, struct nasgpio_led, led_cdev);
  134. }
  135. static struct nasgpio_led *get_led_named(char *name)
  136. {
  137. int i;
  138. for (i = 0; i < ARRAY_SIZE(nasgpio_leds); i++) {
  139. if (strcmp(nasgpio_leds[i].name, name))
  140. continue;
  141. return &nasgpio_leds[i];
  142. }
  143. return NULL;
  144. }
  145. /*
  146. * This protects access to the gpio ports.
  147. */
  148. static DEFINE_SPINLOCK(nasgpio_gpio_lock);
  149. /*
  150. * There are two gpio ports, one for blinking and the other
  151. * for power. @port tells us if we're doing blinking or
  152. * power control.
  153. *
  154. * Caller must hold nasgpio_gpio_lock
  155. */
  156. static void __nasgpio_led_set_attr(struct led_classdev *led_cdev,
  157. u32 port, u32 value)
  158. {
  159. struct nasgpio_led *led = led_classdev_to_nasgpio_led(led_cdev);
  160. u32 gpio_out;
  161. gpio_out = inl(nas_gpio_io_base + port);
  162. if (value)
  163. gpio_out |= (1<<led->gpio_bit);
  164. else
  165. gpio_out &= ~(1<<led->gpio_bit);
  166. outl(gpio_out, nas_gpio_io_base + port);
  167. }
  168. static void nasgpio_led_set_attr(struct led_classdev *led_cdev,
  169. u32 port, u32 value)
  170. {
  171. spin_lock(&nasgpio_gpio_lock);
  172. __nasgpio_led_set_attr(led_cdev, port, value);
  173. spin_unlock(&nasgpio_gpio_lock);
  174. }
  175. static u32 nasgpio_led_get_attr(struct led_classdev *led_cdev, u32 port)
  176. {
  177. struct nasgpio_led *led = led_classdev_to_nasgpio_led(led_cdev);
  178. u32 gpio_in;
  179. spin_lock(&nasgpio_gpio_lock);
  180. gpio_in = inl(nas_gpio_io_base + port);
  181. spin_unlock(&nasgpio_gpio_lock);
  182. if (gpio_in & (1<<led->gpio_bit))
  183. return 1;
  184. return 0;
  185. }
  186. /*
  187. * There is actual brightness control in the hardware,
  188. * but it is via smbus commands and not implemented
  189. * in this driver.
  190. */
  191. static void nasgpio_led_set_brightness(struct led_classdev *led_cdev,
  192. enum led_brightness brightness)
  193. {
  194. u32 setting = 0;
  195. if (brightness >= LED_HALF)
  196. setting = 1;
  197. /*
  198. * Hold the lock across both operations. This ensures
  199. * consistency so that both the "turn off blinking"
  200. * and "turn light off" operations complete as a set.
  201. */
  202. spin_lock(&nasgpio_gpio_lock);
  203. /*
  204. * LED class documentation asks that past blink state
  205. * be disabled when brightness is turned to zero.
  206. */
  207. if (brightness == 0)
  208. __nasgpio_led_set_attr(led_cdev, GPO_BLINK, 0);
  209. __nasgpio_led_set_attr(led_cdev, GP_LVL, setting);
  210. spin_unlock(&nasgpio_gpio_lock);
  211. }
  212. static int nasgpio_led_set_blink(struct led_classdev *led_cdev,
  213. unsigned long *delay_on,
  214. unsigned long *delay_off)
  215. {
  216. u32 setting = 1;
  217. if (!(*delay_on == 0 && *delay_off == 0) &&
  218. !(*delay_on == 500 && *delay_off == 500))
  219. return -EINVAL;
  220. /*
  221. * These are very approximate.
  222. */
  223. *delay_on = 500;
  224. *delay_off = 500;
  225. nasgpio_led_set_attr(led_cdev, GPO_BLINK, setting);
  226. return 0;
  227. }
  228. /*
  229. * Initialize the ICH7 GPIO registers for NAS usage. The BIOS should have
  230. * already taken care of this, but we will do so in a non destructive manner
  231. * so that we have what we need whether the BIOS did it or not.
  232. */
  233. static int ich7_gpio_init(struct device *dev)
  234. {
  235. int i;
  236. u32 config_data = 0;
  237. u32 all_nas_led = 0;
  238. for (i = 0; i < ARRAY_SIZE(nasgpio_leds); i++)
  239. all_nas_led |= (1<<nasgpio_leds[i].gpio_bit);
  240. spin_lock(&nasgpio_gpio_lock);
  241. /*
  242. * We need to enable all of the GPIO lines used by the NAS box,
  243. * so we will read the current Use Selection and add our usage
  244. * to it. This should be benign with regard to the original
  245. * BIOS configuration.
  246. */
  247. config_data = inl(nas_gpio_io_base + GPIO_USE_SEL);
  248. dev_dbg(dev, ": Data read from GPIO_USE_SEL = 0x%08x\n", config_data);
  249. config_data |= all_nas_led + NAS_RECOVERY;
  250. outl(config_data, nas_gpio_io_base + GPIO_USE_SEL);
  251. config_data = inl(nas_gpio_io_base + GPIO_USE_SEL);
  252. dev_dbg(dev, ": GPIO_USE_SEL = 0x%08x\n\n", config_data);
  253. /*
  254. * The LED GPIO outputs need to be configured for output, so we
  255. * will ensure that all LED lines are cleared for output and the
  256. * RECOVERY line ready for input. This too should be benign with
  257. * regard to BIOS configuration.
  258. */
  259. config_data = inl(nas_gpio_io_base + GP_IO_SEL);
  260. dev_dbg(dev, ": Data read from GP_IO_SEL = 0x%08x\n",
  261. config_data);
  262. config_data &= ~all_nas_led;
  263. config_data |= NAS_RECOVERY;
  264. outl(config_data, nas_gpio_io_base + GP_IO_SEL);
  265. config_data = inl(nas_gpio_io_base + GP_IO_SEL);
  266. dev_dbg(dev, ": GP_IO_SEL = 0x%08x\n", config_data);
  267. /*
  268. * In our final system, the BIOS will initialize the state of all
  269. * of the LEDs. For now, we turn them all off (or Low).
  270. */
  271. config_data = inl(nas_gpio_io_base + GP_LVL);
  272. dev_dbg(dev, ": Data read from GP_LVL = 0x%08x\n", config_data);
  273. /*
  274. * In our final system, the BIOS will initialize the blink state of all
  275. * of the LEDs. For now, we turn blink off for all of them.
  276. */
  277. config_data = inl(nas_gpio_io_base + GPO_BLINK);
  278. dev_dbg(dev, ": Data read from GPO_BLINK = 0x%08x\n", config_data);
  279. /*
  280. * At this moment, I am unsure if anything needs to happen with GPI_INV
  281. */
  282. config_data = inl(nas_gpio_io_base + GPI_INV);
  283. dev_dbg(dev, ": Data read from GPI_INV = 0x%08x\n", config_data);
  284. spin_unlock(&nasgpio_gpio_lock);
  285. return 0;
  286. }
  287. static void ich7_lpc_cleanup(struct device *dev)
  288. {
  289. /*
  290. * If we were given exclusive use of the GPIO
  291. * I/O Address range, we must return it.
  292. */
  293. if (gp_gpio_resource) {
  294. dev_dbg(dev, ": Releasing GPIO I/O addresses\n");
  295. release_region(nas_gpio_io_base, ICH7_GPIO_SIZE);
  296. gp_gpio_resource = NULL;
  297. }
  298. }
  299. /*
  300. * The OS has determined that the LPC of the Intel ICH7 Southbridge is present
  301. * so we can retrive the required operational information and prepare the GPIO.
  302. */
  303. static struct pci_dev *nas_gpio_pci_dev;
  304. static int ich7_lpc_probe(struct pci_dev *dev,
  305. const struct pci_device_id *id)
  306. {
  307. int status;
  308. u32 gc = 0;
  309. status = pci_enable_device(dev);
  310. if (status) {
  311. dev_err(&dev->dev, "pci_enable_device failed\n");
  312. return -EIO;
  313. }
  314. nas_gpio_pci_dev = dev;
  315. status = pci_read_config_dword(dev, PMBASE, &g_pm_io_base);
  316. if (status)
  317. goto out;
  318. g_pm_io_base &= 0x00000ff80;
  319. status = pci_read_config_dword(dev, GPIO_CTRL, &gc);
  320. if (!(GPIO_EN & gc)) {
  321. status = -EEXIST;
  322. dev_info(&dev->dev,
  323. "ERROR: The LPC GPIO Block has not been enabled.\n");
  324. goto out;
  325. }
  326. status = pci_read_config_dword(dev, GPIO_BASE, &nas_gpio_io_base);
  327. if (0 > status) {
  328. dev_info(&dev->dev, "Unable to read GPIOBASE.\n");
  329. goto out;
  330. }
  331. dev_dbg(&dev->dev, ": GPIOBASE = 0x%08x\n", nas_gpio_io_base);
  332. nas_gpio_io_base &= 0x00000ffc0;
  333. /*
  334. * Insure that we have exclusive access to the GPIO I/O address range.
  335. */
  336. gp_gpio_resource = request_region(nas_gpio_io_base, ICH7_GPIO_SIZE,
  337. KBUILD_MODNAME);
  338. if (NULL == gp_gpio_resource) {
  339. dev_info(&dev->dev,
  340. "ERROR Unable to register GPIO I/O addresses.\n");
  341. status = -1;
  342. goto out;
  343. }
  344. /*
  345. * Initialize the GPIO for NAS/Home Server Use
  346. */
  347. ich7_gpio_init(&dev->dev);
  348. out:
  349. if (status) {
  350. ich7_lpc_cleanup(&dev->dev);
  351. pci_disable_device(dev);
  352. }
  353. return status;
  354. }
  355. static void ich7_lpc_remove(struct pci_dev *dev)
  356. {
  357. ich7_lpc_cleanup(&dev->dev);
  358. pci_disable_device(dev);
  359. }
  360. /*
  361. * pci_driver structure passed to the PCI modules
  362. */
  363. static struct pci_driver nas_gpio_pci_driver = {
  364. .name = KBUILD_MODNAME,
  365. .id_table = ich7_lpc_pci_id,
  366. .probe = ich7_lpc_probe,
  367. .remove = ich7_lpc_remove,
  368. };
  369. static struct led_classdev *get_classdev_for_led_nr(int nr)
  370. {
  371. struct nasgpio_led *nas_led = &nasgpio_leds[nr];
  372. struct led_classdev *led = &nas_led->led_cdev;
  373. return led;
  374. }
  375. static void set_power_light_amber_noblink(void)
  376. {
  377. struct nasgpio_led *amber = get_led_named("power:amber:power");
  378. struct nasgpio_led *blue = get_led_named("power:blue:power");
  379. if (!amber || !blue)
  380. return;
  381. /*
  382. * LED_OFF implies disabling future blinking
  383. */
  384. pr_debug("setting blue off and amber on\n");
  385. nasgpio_led_set_brightness(&blue->led_cdev, LED_OFF);
  386. nasgpio_led_set_brightness(&amber->led_cdev, LED_FULL);
  387. }
  388. static ssize_t nas_led_blink_show(struct device *dev,
  389. struct device_attribute *attr, char *buf)
  390. {
  391. struct led_classdev *led = dev_get_drvdata(dev);
  392. int blinking = 0;
  393. if (nasgpio_led_get_attr(led, GPO_BLINK))
  394. blinking = 1;
  395. return sprintf(buf, "%u\n", blinking);
  396. }
  397. static ssize_t nas_led_blink_store(struct device *dev,
  398. struct device_attribute *attr,
  399. const char *buf, size_t size)
  400. {
  401. int ret;
  402. struct led_classdev *led = dev_get_drvdata(dev);
  403. unsigned long blink_state;
  404. ret = kstrtoul(buf, 10, &blink_state);
  405. if (ret)
  406. return ret;
  407. nasgpio_led_set_attr(led, GPO_BLINK, blink_state);
  408. return size;
  409. }
  410. static DEVICE_ATTR(blink, 0644, nas_led_blink_show, nas_led_blink_store);
  411. static struct attribute *nasgpio_led_attrs[] = {
  412. &dev_attr_blink.attr,
  413. NULL
  414. };
  415. ATTRIBUTE_GROUPS(nasgpio_led);
  416. static int register_nasgpio_led(int led_nr)
  417. {
  418. int ret;
  419. struct nasgpio_led *nas_led = &nasgpio_leds[led_nr];
  420. struct led_classdev *led = get_classdev_for_led_nr(led_nr);
  421. led->name = nas_led->name;
  422. led->brightness = LED_OFF;
  423. if (nasgpio_led_get_attr(led, GP_LVL))
  424. led->brightness = LED_FULL;
  425. led->brightness_set = nasgpio_led_set_brightness;
  426. led->blink_set = nasgpio_led_set_blink;
  427. led->groups = nasgpio_led_groups;
  428. ret = led_classdev_register(&nas_gpio_pci_dev->dev, led);
  429. if (ret)
  430. return ret;
  431. return 0;
  432. }
  433. static void unregister_nasgpio_led(int led_nr)
  434. {
  435. struct led_classdev *led = get_classdev_for_led_nr(led_nr);
  436. led_classdev_unregister(led);
  437. }
  438. /*
  439. * module load/initialization
  440. */
  441. static int __init nas_gpio_init(void)
  442. {
  443. int i;
  444. int ret = 0;
  445. int nr_devices = 0;
  446. nr_devices = dmi_check_system(nas_led_whitelist);
  447. if (nodetect) {
  448. pr_info("skipping hardware autodetection\n");
  449. pr_info("Please send 'dmidecode' output to dave@sr71.net\n");
  450. nr_devices++;
  451. }
  452. if (nr_devices <= 0) {
  453. pr_info("no LED devices found\n");
  454. return -ENODEV;
  455. }
  456. pr_info("registering PCI driver\n");
  457. ret = pci_register_driver(&nas_gpio_pci_driver);
  458. if (ret)
  459. return ret;
  460. for (i = 0; i < ARRAY_SIZE(nasgpio_leds); i++) {
  461. ret = register_nasgpio_led(i);
  462. if (ret)
  463. goto out_err;
  464. }
  465. /*
  466. * When the system powers on, the BIOS leaves the power
  467. * light blue and blinking. This will turn it solid
  468. * amber once the driver is loaded.
  469. */
  470. set_power_light_amber_noblink();
  471. return 0;
  472. out_err:
  473. for (i--; i >= 0; i--)
  474. unregister_nasgpio_led(i);
  475. pci_unregister_driver(&nas_gpio_pci_driver);
  476. return ret;
  477. }
  478. /*
  479. * module unload
  480. */
  481. static void __exit nas_gpio_exit(void)
  482. {
  483. int i;
  484. pr_info("Unregistering driver\n");
  485. for (i = 0; i < ARRAY_SIZE(nasgpio_leds); i++)
  486. unregister_nasgpio_led(i);
  487. pci_unregister_driver(&nas_gpio_pci_driver);
  488. }
  489. module_init(nas_gpio_init);
  490. module_exit(nas_gpio_exit);