ics932s401.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * A driver for the Integrated Circuits ICS932S401
  3. * Copyright (C) 2008 IBM
  4. *
  5. * Author: Darrick J. Wong <djwong@us.ibm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/module.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/i2c.h>
  24. #include <linux/err.h>
  25. #include <linux/mutex.h>
  26. #include <linux/delay.h>
  27. #include <linux/log2.h>
  28. /* Addresses to scan */
  29. static const unsigned short normal_i2c[] = { 0x69, I2C_CLIENT_END };
  30. /* Insmod parameters */
  31. I2C_CLIENT_INSMOD_1(ics932s401);
  32. /* ICS932S401 registers */
  33. #define ICS932S401_REG_CFG2 0x01
  34. #define ICS932S401_CFG1_SPREAD 0x01
  35. #define ICS932S401_REG_CFG7 0x06
  36. #define ICS932S401_FS_MASK 0x07
  37. #define ICS932S401_REG_VENDOR_REV 0x07
  38. #define ICS932S401_VENDOR 1
  39. #define ICS932S401_VENDOR_MASK 0x0F
  40. #define ICS932S401_REV 4
  41. #define ICS932S401_REV_SHIFT 4
  42. #define ICS932S401_REG_DEVICE 0x09
  43. #define ICS932S401_DEVICE 11
  44. #define ICS932S401_REG_CTRL 0x0A
  45. #define ICS932S401_MN_ENABLED 0x80
  46. #define ICS932S401_CPU_ALT 0x04
  47. #define ICS932S401_SRC_ALT 0x08
  48. #define ICS932S401_REG_CPU_M_CTRL 0x0B
  49. #define ICS932S401_M_MASK 0x3F
  50. #define ICS932S401_REG_CPU_N_CTRL 0x0C
  51. #define ICS932S401_REG_CPU_SPREAD1 0x0D
  52. #define ICS932S401_REG_CPU_SPREAD2 0x0E
  53. #define ICS932S401_SPREAD_MASK 0x7FFF
  54. #define ICS932S401_REG_SRC_M_CTRL 0x0F
  55. #define ICS932S401_REG_SRC_N_CTRL 0x10
  56. #define ICS932S401_REG_SRC_SPREAD1 0x11
  57. #define ICS932S401_REG_SRC_SPREAD2 0x12
  58. #define ICS932S401_REG_CPU_DIVISOR 0x13
  59. #define ICS932S401_CPU_DIVISOR_SHIFT 4
  60. #define ICS932S401_REG_PCISRC_DIVISOR 0x14
  61. #define ICS932S401_SRC_DIVISOR_MASK 0x0F
  62. #define ICS932S401_PCI_DIVISOR_SHIFT 4
  63. /* Base clock is 14.318MHz */
  64. #define BASE_CLOCK 14318
  65. #define NUM_REGS 21
  66. #define NUM_MIRRORED_REGS 15
  67. static int regs_to_copy[NUM_MIRRORED_REGS] = {
  68. ICS932S401_REG_CFG2,
  69. ICS932S401_REG_CFG7,
  70. ICS932S401_REG_VENDOR_REV,
  71. ICS932S401_REG_DEVICE,
  72. ICS932S401_REG_CTRL,
  73. ICS932S401_REG_CPU_M_CTRL,
  74. ICS932S401_REG_CPU_N_CTRL,
  75. ICS932S401_REG_CPU_SPREAD1,
  76. ICS932S401_REG_CPU_SPREAD2,
  77. ICS932S401_REG_SRC_M_CTRL,
  78. ICS932S401_REG_SRC_N_CTRL,
  79. ICS932S401_REG_SRC_SPREAD1,
  80. ICS932S401_REG_SRC_SPREAD2,
  81. ICS932S401_REG_CPU_DIVISOR,
  82. ICS932S401_REG_PCISRC_DIVISOR,
  83. };
  84. /* How often do we reread sensors values? (In jiffies) */
  85. #define SENSOR_REFRESH_INTERVAL (2 * HZ)
  86. /* How often do we reread sensor limit values? (In jiffies) */
  87. #define LIMIT_REFRESH_INTERVAL (60 * HZ)
  88. struct ics932s401_data {
  89. struct attribute_group attrs;
  90. struct mutex lock;
  91. char sensors_valid;
  92. unsigned long sensors_last_updated; /* In jiffies */
  93. u8 regs[NUM_REGS];
  94. };
  95. static int ics932s401_probe(struct i2c_client *client,
  96. const struct i2c_device_id *id);
  97. static int ics932s401_detect(struct i2c_client *client, int kind,
  98. struct i2c_board_info *info);
  99. static int ics932s401_remove(struct i2c_client *client);
  100. static const struct i2c_device_id ics932s401_id[] = {
  101. { "ics932s401", ics932s401 },
  102. { }
  103. };
  104. MODULE_DEVICE_TABLE(i2c, ics932s401_id);
  105. static struct i2c_driver ics932s401_driver = {
  106. .class = I2C_CLASS_HWMON,
  107. .driver = {
  108. .name = "ics932s401",
  109. },
  110. .probe = ics932s401_probe,
  111. .remove = ics932s401_remove,
  112. .id_table = ics932s401_id,
  113. .detect = ics932s401_detect,
  114. .address_data = &addr_data,
  115. };
  116. static struct ics932s401_data *ics932s401_update_device(struct device *dev)
  117. {
  118. struct i2c_client *client = to_i2c_client(dev);
  119. struct ics932s401_data *data = i2c_get_clientdata(client);
  120. unsigned long local_jiffies = jiffies;
  121. int i, temp;
  122. mutex_lock(&data->lock);
  123. if (time_before(local_jiffies, data->sensors_last_updated +
  124. SENSOR_REFRESH_INTERVAL)
  125. && data->sensors_valid)
  126. goto out;
  127. /*
  128. * Each register must be read as a word and then right shifted 8 bits.
  129. * Not really sure why this is; setting the "byte count programming"
  130. * register to 1 does not fix this problem.
  131. */
  132. for (i = 0; i < NUM_MIRRORED_REGS; i++) {
  133. temp = i2c_smbus_read_word_data(client, regs_to_copy[i]);
  134. data->regs[regs_to_copy[i]] = temp >> 8;
  135. }
  136. data->sensors_last_updated = local_jiffies;
  137. data->sensors_valid = 1;
  138. out:
  139. mutex_unlock(&data->lock);
  140. return data;
  141. }
  142. static ssize_t show_spread_enabled(struct device *dev,
  143. struct device_attribute *devattr,
  144. char *buf)
  145. {
  146. struct ics932s401_data *data = ics932s401_update_device(dev);
  147. if (data->regs[ICS932S401_REG_CFG2] & ICS932S401_CFG1_SPREAD)
  148. return sprintf(buf, "1\n");
  149. return sprintf(buf, "0\n");
  150. }
  151. /* bit to cpu khz map */
  152. static const int fs_speeds[] = {
  153. 266666,
  154. 133333,
  155. 200000,
  156. 166666,
  157. 333333,
  158. 100000,
  159. 400000,
  160. 0,
  161. };
  162. /* clock divisor map */
  163. static const int divisors[] = {2, 3, 5, 15, 4, 6, 10, 30, 8, 12, 20, 60, 16,
  164. 24, 40, 120};
  165. /* Calculate CPU frequency from the M/N registers. */
  166. static int calculate_cpu_freq(struct ics932s401_data *data)
  167. {
  168. int m, n, freq;
  169. m = data->regs[ICS932S401_REG_CPU_M_CTRL] & ICS932S401_M_MASK;
  170. n = data->regs[ICS932S401_REG_CPU_N_CTRL];
  171. /* Pull in bits 8 & 9 from the M register */
  172. n |= ((int)data->regs[ICS932S401_REG_CPU_M_CTRL] & 0x80) << 1;
  173. n |= ((int)data->regs[ICS932S401_REG_CPU_M_CTRL] & 0x40) << 3;
  174. freq = BASE_CLOCK * (n + 8) / (m + 2);
  175. freq /= divisors[data->regs[ICS932S401_REG_CPU_DIVISOR] >>
  176. ICS932S401_CPU_DIVISOR_SHIFT];
  177. return freq;
  178. }
  179. static ssize_t show_cpu_clock(struct device *dev,
  180. struct device_attribute *devattr,
  181. char *buf)
  182. {
  183. struct ics932s401_data *data = ics932s401_update_device(dev);
  184. return sprintf(buf, "%d\n", calculate_cpu_freq(data));
  185. }
  186. static ssize_t show_cpu_clock_sel(struct device *dev,
  187. struct device_attribute *devattr,
  188. char *buf)
  189. {
  190. struct ics932s401_data *data = ics932s401_update_device(dev);
  191. int freq;
  192. if (data->regs[ICS932S401_REG_CTRL] & ICS932S401_MN_ENABLED)
  193. freq = calculate_cpu_freq(data);
  194. else {
  195. /* Freq is neatly wrapped up for us */
  196. int fid = data->regs[ICS932S401_REG_CFG7] & ICS932S401_FS_MASK;
  197. freq = fs_speeds[fid];
  198. if (data->regs[ICS932S401_REG_CTRL] & ICS932S401_CPU_ALT) {
  199. switch (freq) {
  200. case 166666:
  201. freq = 160000;
  202. break;
  203. case 333333:
  204. freq = 320000;
  205. break;
  206. }
  207. }
  208. }
  209. return sprintf(buf, "%d\n", freq);
  210. }
  211. /* Calculate SRC frequency from the M/N registers. */
  212. static int calculate_src_freq(struct ics932s401_data *data)
  213. {
  214. int m, n, freq;
  215. m = data->regs[ICS932S401_REG_SRC_M_CTRL] & ICS932S401_M_MASK;
  216. n = data->regs[ICS932S401_REG_SRC_N_CTRL];
  217. /* Pull in bits 8 & 9 from the M register */
  218. n |= ((int)data->regs[ICS932S401_REG_SRC_M_CTRL] & 0x80) << 1;
  219. n |= ((int)data->regs[ICS932S401_REG_SRC_M_CTRL] & 0x40) << 3;
  220. freq = BASE_CLOCK * (n + 8) / (m + 2);
  221. freq /= divisors[data->regs[ICS932S401_REG_PCISRC_DIVISOR] &
  222. ICS932S401_SRC_DIVISOR_MASK];
  223. return freq;
  224. }
  225. static ssize_t show_src_clock(struct device *dev,
  226. struct device_attribute *devattr,
  227. char *buf)
  228. {
  229. struct ics932s401_data *data = ics932s401_update_device(dev);
  230. return sprintf(buf, "%d\n", calculate_src_freq(data));
  231. }
  232. static ssize_t show_src_clock_sel(struct device *dev,
  233. struct device_attribute *devattr,
  234. char *buf)
  235. {
  236. struct ics932s401_data *data = ics932s401_update_device(dev);
  237. int freq;
  238. if (data->regs[ICS932S401_REG_CTRL] & ICS932S401_MN_ENABLED)
  239. freq = calculate_src_freq(data);
  240. else
  241. /* Freq is neatly wrapped up for us */
  242. if (data->regs[ICS932S401_REG_CTRL] & ICS932S401_CPU_ALT &&
  243. data->regs[ICS932S401_REG_CTRL] & ICS932S401_SRC_ALT)
  244. freq = 96000;
  245. else
  246. freq = 100000;
  247. return sprintf(buf, "%d\n", freq);
  248. }
  249. /* Calculate PCI frequency from the SRC M/N registers. */
  250. static int calculate_pci_freq(struct ics932s401_data *data)
  251. {
  252. int m, n, freq;
  253. m = data->regs[ICS932S401_REG_SRC_M_CTRL] & ICS932S401_M_MASK;
  254. n = data->regs[ICS932S401_REG_SRC_N_CTRL];
  255. /* Pull in bits 8 & 9 from the M register */
  256. n |= ((int)data->regs[ICS932S401_REG_SRC_M_CTRL] & 0x80) << 1;
  257. n |= ((int)data->regs[ICS932S401_REG_SRC_M_CTRL] & 0x40) << 3;
  258. freq = BASE_CLOCK * (n + 8) / (m + 2);
  259. freq /= divisors[data->regs[ICS932S401_REG_PCISRC_DIVISOR] >>
  260. ICS932S401_PCI_DIVISOR_SHIFT];
  261. return freq;
  262. }
  263. static ssize_t show_pci_clock(struct device *dev,
  264. struct device_attribute *devattr,
  265. char *buf)
  266. {
  267. struct ics932s401_data *data = ics932s401_update_device(dev);
  268. return sprintf(buf, "%d\n", calculate_pci_freq(data));
  269. }
  270. static ssize_t show_pci_clock_sel(struct device *dev,
  271. struct device_attribute *devattr,
  272. char *buf)
  273. {
  274. struct ics932s401_data *data = ics932s401_update_device(dev);
  275. int freq;
  276. if (data->regs[ICS932S401_REG_CTRL] & ICS932S401_MN_ENABLED)
  277. freq = calculate_pci_freq(data);
  278. else
  279. freq = 33333;
  280. return sprintf(buf, "%d\n", freq);
  281. }
  282. static ssize_t show_value(struct device *dev,
  283. struct device_attribute *devattr,
  284. char *buf);
  285. static ssize_t show_spread(struct device *dev,
  286. struct device_attribute *devattr,
  287. char *buf);
  288. static DEVICE_ATTR(spread_enabled, S_IRUGO, show_spread_enabled, NULL);
  289. static DEVICE_ATTR(cpu_clock_selection, S_IRUGO, show_cpu_clock_sel, NULL);
  290. static DEVICE_ATTR(cpu_clock, S_IRUGO, show_cpu_clock, NULL);
  291. static DEVICE_ATTR(src_clock_selection, S_IRUGO, show_src_clock_sel, NULL);
  292. static DEVICE_ATTR(src_clock, S_IRUGO, show_src_clock, NULL);
  293. static DEVICE_ATTR(pci_clock_selection, S_IRUGO, show_pci_clock_sel, NULL);
  294. static DEVICE_ATTR(pci_clock, S_IRUGO, show_pci_clock, NULL);
  295. static DEVICE_ATTR(usb_clock, S_IRUGO, show_value, NULL);
  296. static DEVICE_ATTR(ref_clock, S_IRUGO, show_value, NULL);
  297. static DEVICE_ATTR(cpu_spread, S_IRUGO, show_spread, NULL);
  298. static DEVICE_ATTR(src_spread, S_IRUGO, show_spread, NULL);
  299. static struct attribute *ics932s401_attr[] =
  300. {
  301. &dev_attr_spread_enabled.attr,
  302. &dev_attr_cpu_clock_selection.attr,
  303. &dev_attr_cpu_clock.attr,
  304. &dev_attr_src_clock_selection.attr,
  305. &dev_attr_src_clock.attr,
  306. &dev_attr_pci_clock_selection.attr,
  307. &dev_attr_pci_clock.attr,
  308. &dev_attr_usb_clock.attr,
  309. &dev_attr_ref_clock.attr,
  310. &dev_attr_cpu_spread.attr,
  311. &dev_attr_src_spread.attr,
  312. NULL
  313. };
  314. static ssize_t show_value(struct device *dev,
  315. struct device_attribute *devattr,
  316. char *buf)
  317. {
  318. int x;
  319. if (devattr == &dev_attr_usb_clock)
  320. x = 48000;
  321. else if (devattr == &dev_attr_ref_clock)
  322. x = BASE_CLOCK;
  323. else
  324. BUG();
  325. return sprintf(buf, "%d\n", x);
  326. }
  327. static ssize_t show_spread(struct device *dev,
  328. struct device_attribute *devattr,
  329. char *buf)
  330. {
  331. struct ics932s401_data *data = ics932s401_update_device(dev);
  332. int reg;
  333. unsigned long val;
  334. if (!(data->regs[ICS932S401_REG_CFG2] & ICS932S401_CFG1_SPREAD))
  335. return sprintf(buf, "0%%\n");
  336. if (devattr == &dev_attr_src_spread)
  337. reg = ICS932S401_REG_SRC_SPREAD1;
  338. else if (devattr == &dev_attr_cpu_spread)
  339. reg = ICS932S401_REG_CPU_SPREAD1;
  340. else
  341. BUG();
  342. val = data->regs[reg] | (data->regs[reg + 1] << 8);
  343. val &= ICS932S401_SPREAD_MASK;
  344. /* Scale 0..2^14 to -0.5. */
  345. val = 500000 * val / 16384;
  346. return sprintf(buf, "-0.%lu%%\n", val);
  347. }
  348. /* Return 0 if detection is successful, -ENODEV otherwise */
  349. static int ics932s401_detect(struct i2c_client *client, int kind,
  350. struct i2c_board_info *info)
  351. {
  352. struct i2c_adapter *adapter = client->adapter;
  353. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  354. return -ENODEV;
  355. if (kind <= 0) {
  356. int vendor, device, revision;
  357. vendor = i2c_smbus_read_word_data(client,
  358. ICS932S401_REG_VENDOR_REV);
  359. vendor >>= 8;
  360. revision = vendor >> ICS932S401_REV_SHIFT;
  361. vendor &= ICS932S401_VENDOR_MASK;
  362. if (vendor != ICS932S401_VENDOR)
  363. return -ENODEV;
  364. device = i2c_smbus_read_word_data(client,
  365. ICS932S401_REG_DEVICE);
  366. device >>= 8;
  367. if (device != ICS932S401_DEVICE)
  368. return -ENODEV;
  369. if (revision != ICS932S401_REV)
  370. dev_info(&adapter->dev, "Unknown revision %d\n",
  371. revision);
  372. } else
  373. dev_dbg(&adapter->dev, "detection forced\n");
  374. strlcpy(info->type, "ics932s401", I2C_NAME_SIZE);
  375. return 0;
  376. }
  377. static int ics932s401_probe(struct i2c_client *client,
  378. const struct i2c_device_id *id)
  379. {
  380. struct ics932s401_data *data;
  381. int err;
  382. data = kzalloc(sizeof(struct ics932s401_data), GFP_KERNEL);
  383. if (!data) {
  384. err = -ENOMEM;
  385. goto exit;
  386. }
  387. i2c_set_clientdata(client, data);
  388. mutex_init(&data->lock);
  389. dev_info(&client->dev, "%s chip found\n", client->name);
  390. /* Register sysfs hooks */
  391. data->attrs.attrs = ics932s401_attr;
  392. err = sysfs_create_group(&client->dev.kobj, &data->attrs);
  393. if (err)
  394. goto exit_free;
  395. return 0;
  396. exit_free:
  397. kfree(data);
  398. exit:
  399. return err;
  400. }
  401. static int ics932s401_remove(struct i2c_client *client)
  402. {
  403. struct ics932s401_data *data = i2c_get_clientdata(client);
  404. sysfs_remove_group(&client->dev.kobj, &data->attrs);
  405. kfree(data);
  406. return 0;
  407. }
  408. static int __init ics932s401_init(void)
  409. {
  410. return i2c_add_driver(&ics932s401_driver);
  411. }
  412. static void __exit ics932s401_exit(void)
  413. {
  414. i2c_del_driver(&ics932s401_driver);
  415. }
  416. MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
  417. MODULE_DESCRIPTION("ICS932S401 driver");
  418. MODULE_LICENSE("GPL");
  419. module_init(ics932s401_init);
  420. module_exit(ics932s401_exit);
  421. /* IBM IntelliStation Z30 */
  422. MODULE_ALIAS("dmi:bvnIBM:*:rn9228:*");
  423. MODULE_ALIAS("dmi:bvnIBM:*:rn9232:*");
  424. /* IBM x3650/x3550 */
  425. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650*");
  426. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550*");