asc7621.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. /*
  2. * asc7621.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
  3. * Copyright (c) 2007, 2010 George Joseph <george.joseph@fairview5.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/i2c.h>
  24. #include <linux/hwmon.h>
  25. #include <linux/hwmon-sysfs.h>
  26. #include <linux/err.h>
  27. #include <linux/mutex.h>
  28. /* Addresses to scan */
  29. static const unsigned short normal_i2c[] = {
  30. 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
  31. };
  32. enum asc7621_type {
  33. asc7621,
  34. asc7621a
  35. };
  36. #define INTERVAL_HIGH (HZ + HZ / 2)
  37. #define INTERVAL_LOW (1 * 60 * HZ)
  38. #define PRI_NONE 0
  39. #define PRI_LOW 1
  40. #define PRI_HIGH 2
  41. #define FIRST_CHIP asc7621
  42. #define LAST_CHIP asc7621a
  43. struct asc7621_chip {
  44. char *name;
  45. enum asc7621_type chip_type;
  46. u8 company_reg;
  47. u8 company_id;
  48. u8 verstep_reg;
  49. u8 verstep_id;
  50. const unsigned short *addresses;
  51. };
  52. static struct asc7621_chip asc7621_chips[] = {
  53. {
  54. .name = "asc7621",
  55. .chip_type = asc7621,
  56. .company_reg = 0x3e,
  57. .company_id = 0x61,
  58. .verstep_reg = 0x3f,
  59. .verstep_id = 0x6c,
  60. .addresses = normal_i2c,
  61. },
  62. {
  63. .name = "asc7621a",
  64. .chip_type = asc7621a,
  65. .company_reg = 0x3e,
  66. .company_id = 0x61,
  67. .verstep_reg = 0x3f,
  68. .verstep_id = 0x6d,
  69. .addresses = normal_i2c,
  70. },
  71. };
  72. /*
  73. * Defines the highest register to be used, not the count.
  74. * The actual count will probably be smaller because of gaps
  75. * in the implementation (unused register locations).
  76. * This define will safely set the array size of both the parameter
  77. * and data arrays.
  78. * This comes from the data sheet register description table.
  79. */
  80. #define LAST_REGISTER 0xff
  81. struct asc7621_data {
  82. struct i2c_client client;
  83. struct device *class_dev;
  84. struct mutex update_lock;
  85. int valid; /* !=0 if following fields are valid */
  86. unsigned long last_high_reading; /* In jiffies */
  87. unsigned long last_low_reading; /* In jiffies */
  88. /*
  89. * Registers we care about occupy the corresponding index
  90. * in the array. Registers we don't care about are left
  91. * at 0.
  92. */
  93. u8 reg[LAST_REGISTER + 1];
  94. };
  95. /*
  96. * Macro to get the parent asc7621_param structure
  97. * from a sensor_device_attribute passed into the
  98. * show/store functions.
  99. */
  100. #define to_asc7621_param(_sda) \
  101. container_of(_sda, struct asc7621_param, sda)
  102. /*
  103. * Each parameter to be retrieved needs an asc7621_param structure
  104. * allocated. It contains the sensor_device_attribute structure
  105. * and the control info needed to retrieve the value from the register map.
  106. */
  107. struct asc7621_param {
  108. struct sensor_device_attribute sda;
  109. u8 priority;
  110. u8 msb[3];
  111. u8 lsb[3];
  112. u8 mask[3];
  113. u8 shift[3];
  114. };
  115. /*
  116. * This is the map that ultimately indicates whether we'll be
  117. * retrieving a register value or not, and at what frequency.
  118. */
  119. static u8 asc7621_register_priorities[255];
  120. static struct asc7621_data *asc7621_update_device(struct device *dev);
  121. static inline u8 read_byte(struct i2c_client *client, u8 reg)
  122. {
  123. int res = i2c_smbus_read_byte_data(client, reg);
  124. if (res < 0) {
  125. dev_err(&client->dev,
  126. "Unable to read from register 0x%02x.\n", reg);
  127. return 0;
  128. };
  129. return res & 0xff;
  130. }
  131. static inline int write_byte(struct i2c_client *client, u8 reg, u8 data)
  132. {
  133. int res = i2c_smbus_write_byte_data(client, reg, data);
  134. if (res < 0) {
  135. dev_err(&client->dev,
  136. "Unable to write value 0x%02x to register 0x%02x.\n",
  137. data, reg);
  138. };
  139. return res;
  140. }
  141. /*
  142. * Data Handlers
  143. * Each function handles the formatting, storage
  144. * and retrieval of like parameters.
  145. */
  146. #define SETUP_SHOW_data_param(d, a) \
  147. struct sensor_device_attribute *sda = to_sensor_dev_attr(a); \
  148. struct asc7621_data *data = asc7621_update_device(d); \
  149. struct asc7621_param *param = to_asc7621_param(sda)
  150. #define SETUP_STORE_data_param(d, a) \
  151. struct sensor_device_attribute *sda = to_sensor_dev_attr(a); \
  152. struct i2c_client *client = to_i2c_client(d); \
  153. struct asc7621_data *data = i2c_get_clientdata(client); \
  154. struct asc7621_param *param = to_asc7621_param(sda)
  155. /*
  156. * u8 is just what it sounds like...an unsigned byte with no
  157. * special formatting.
  158. */
  159. static ssize_t show_u8(struct device *dev, struct device_attribute *attr,
  160. char *buf)
  161. {
  162. SETUP_SHOW_data_param(dev, attr);
  163. return sprintf(buf, "%u\n", data->reg[param->msb[0]]);
  164. }
  165. static ssize_t store_u8(struct device *dev, struct device_attribute *attr,
  166. const char *buf, size_t count)
  167. {
  168. SETUP_STORE_data_param(dev, attr);
  169. long reqval;
  170. if (strict_strtol(buf, 10, &reqval))
  171. return -EINVAL;
  172. reqval = SENSORS_LIMIT(reqval, 0, 255);
  173. mutex_lock(&data->update_lock);
  174. data->reg[param->msb[0]] = reqval;
  175. write_byte(client, param->msb[0], reqval);
  176. mutex_unlock(&data->update_lock);
  177. return count;
  178. }
  179. /*
  180. * Many of the config values occupy only a few bits of a register.
  181. */
  182. static ssize_t show_bitmask(struct device *dev,
  183. struct device_attribute *attr, char *buf)
  184. {
  185. SETUP_SHOW_data_param(dev, attr);
  186. return sprintf(buf, "%u\n",
  187. (data->reg[param->msb[0]] >> param->
  188. shift[0]) & param->mask[0]);
  189. }
  190. static ssize_t store_bitmask(struct device *dev,
  191. struct device_attribute *attr,
  192. const char *buf, size_t count)
  193. {
  194. SETUP_STORE_data_param(dev, attr);
  195. long reqval;
  196. u8 currval;
  197. if (strict_strtol(buf, 10, &reqval))
  198. return -EINVAL;
  199. reqval = SENSORS_LIMIT(reqval, 0, param->mask[0]);
  200. reqval = (reqval & param->mask[0]) << param->shift[0];
  201. mutex_lock(&data->update_lock);
  202. currval = read_byte(client, param->msb[0]);
  203. reqval |= (currval & ~(param->mask[0] << param->shift[0]));
  204. data->reg[param->msb[0]] = reqval;
  205. write_byte(client, param->msb[0], reqval);
  206. mutex_unlock(&data->update_lock);
  207. return count;
  208. }
  209. /*
  210. * 16 bit fan rpm values
  211. * reported by the device as the number of 11.111us periods (90khz)
  212. * between full fan rotations. Therefore...
  213. * RPM = (90000 * 60) / register value
  214. */
  215. static ssize_t show_fan16(struct device *dev,
  216. struct device_attribute *attr, char *buf)
  217. {
  218. SETUP_SHOW_data_param(dev, attr);
  219. u16 regval;
  220. mutex_lock(&data->update_lock);
  221. regval = (data->reg[param->msb[0]] << 8) | data->reg[param->lsb[0]];
  222. mutex_unlock(&data->update_lock);
  223. return sprintf(buf, "%u\n",
  224. (regval == 0 ? -1 : (regval) ==
  225. 0xffff ? 0 : 5400000 / regval));
  226. }
  227. static ssize_t store_fan16(struct device *dev,
  228. struct device_attribute *attr, const char *buf,
  229. size_t count)
  230. {
  231. SETUP_STORE_data_param(dev, attr);
  232. long reqval;
  233. if (strict_strtol(buf, 10, &reqval))
  234. return -EINVAL;
  235. /* If a minimum RPM of zero is requested, then we set the register to
  236. 0xffff. This value allows the fan to be stopped completely without
  237. generating an alarm. */
  238. reqval =
  239. (reqval <= 0 ? 0xffff : SENSORS_LIMIT(5400000 / reqval, 0, 0xfffe));
  240. mutex_lock(&data->update_lock);
  241. data->reg[param->msb[0]] = (reqval >> 8) & 0xff;
  242. data->reg[param->lsb[0]] = reqval & 0xff;
  243. write_byte(client, param->msb[0], data->reg[param->msb[0]]);
  244. write_byte(client, param->lsb[0], data->reg[param->lsb[0]]);
  245. mutex_unlock(&data->update_lock);
  246. return count;
  247. }
  248. /*
  249. * Voltages are scaled in the device so that the nominal voltage
  250. * is 3/4ths of the 0-255 range (i.e. 192).
  251. * If all voltages are 'normal' then all voltage registers will
  252. * read 0xC0.
  253. *
  254. * The data sheet provides us with the 3/4 scale value for each voltage
  255. * which is stored in in_scaling. The sda->index parameter value provides
  256. * the index into in_scaling.
  257. *
  258. * NOTE: The chip expects the first 2 inputs be 2.5 and 2.25 volts
  259. * respectively. That doesn't mean that's what the motherboard provides. :)
  260. */
  261. static int asc7621_in_scaling[] = {
  262. 2500, 2250, 3300, 5000, 12000
  263. };
  264. static ssize_t show_in10(struct device *dev, struct device_attribute *attr,
  265. char *buf)
  266. {
  267. SETUP_SHOW_data_param(dev, attr);
  268. u16 regval;
  269. u8 nr = sda->index;
  270. mutex_lock(&data->update_lock);
  271. regval = (data->reg[param->msb[0]] << 8) | (data->reg[param->lsb[0]]);
  272. mutex_unlock(&data->update_lock);
  273. /* The LSB value is a 2-bit scaling of the MSB's LSbit value. */
  274. regval = (regval >> 6) * asc7621_in_scaling[nr] / (0xc0 << 2);
  275. return sprintf(buf, "%u\n", regval);
  276. }
  277. /* 8 bit voltage values (the mins and maxs) */
  278. static ssize_t show_in8(struct device *dev, struct device_attribute *attr,
  279. char *buf)
  280. {
  281. SETUP_SHOW_data_param(dev, attr);
  282. u8 nr = sda->index;
  283. return sprintf(buf, "%u\n",
  284. ((data->reg[param->msb[0]] *
  285. asc7621_in_scaling[nr]) / 0xc0));
  286. }
  287. static ssize_t store_in8(struct device *dev, struct device_attribute *attr,
  288. const char *buf, size_t count)
  289. {
  290. SETUP_STORE_data_param(dev, attr);
  291. long reqval;
  292. u8 nr = sda->index;
  293. if (strict_strtol(buf, 10, &reqval))
  294. return -EINVAL;
  295. reqval = SENSORS_LIMIT(reqval, 0, 0xffff);
  296. reqval = reqval * 0xc0 / asc7621_in_scaling[nr];
  297. reqval = SENSORS_LIMIT(reqval, 0, 0xff);
  298. mutex_lock(&data->update_lock);
  299. data->reg[param->msb[0]] = reqval;
  300. write_byte(client, param->msb[0], reqval);
  301. mutex_unlock(&data->update_lock);
  302. return count;
  303. }
  304. static ssize_t show_temp8(struct device *dev,
  305. struct device_attribute *attr, char *buf)
  306. {
  307. SETUP_SHOW_data_param(dev, attr);
  308. return sprintf(buf, "%d\n", ((s8) data->reg[param->msb[0]]) * 1000);
  309. }
  310. static ssize_t store_temp8(struct device *dev,
  311. struct device_attribute *attr, const char *buf,
  312. size_t count)
  313. {
  314. SETUP_STORE_data_param(dev, attr);
  315. long reqval;
  316. s8 temp;
  317. if (strict_strtol(buf, 10, &reqval))
  318. return -EINVAL;
  319. reqval = SENSORS_LIMIT(reqval, -127000, 127000);
  320. temp = reqval / 1000;
  321. mutex_lock(&data->update_lock);
  322. data->reg[param->msb[0]] = temp;
  323. write_byte(client, param->msb[0], temp);
  324. mutex_unlock(&data->update_lock);
  325. return count;
  326. }
  327. /*
  328. * Temperatures that occupy 2 bytes always have the whole
  329. * number of degrees in the MSB with some part of the LSB
  330. * indicating fractional degrees.
  331. */
  332. /* mmmmmmmm.llxxxxxx */
  333. static ssize_t show_temp10(struct device *dev,
  334. struct device_attribute *attr, char *buf)
  335. {
  336. SETUP_SHOW_data_param(dev, attr);
  337. u8 msb, lsb;
  338. int temp;
  339. mutex_lock(&data->update_lock);
  340. msb = data->reg[param->msb[0]];
  341. lsb = (data->reg[param->lsb[0]] >> 6) & 0x03;
  342. temp = (((s8) msb) * 1000) + (lsb * 250);
  343. mutex_unlock(&data->update_lock);
  344. return sprintf(buf, "%d\n", temp);
  345. }
  346. /* mmmmmm.ll */
  347. static ssize_t show_temp62(struct device *dev,
  348. struct device_attribute *attr, char *buf)
  349. {
  350. SETUP_SHOW_data_param(dev, attr);
  351. u8 regval = data->reg[param->msb[0]];
  352. int temp = ((s8) (regval & 0xfc) * 1000) + ((regval & 0x03) * 250);
  353. return sprintf(buf, "%d\n", temp);
  354. }
  355. static ssize_t store_temp62(struct device *dev,
  356. struct device_attribute *attr, const char *buf,
  357. size_t count)
  358. {
  359. SETUP_STORE_data_param(dev, attr);
  360. long reqval, i, f;
  361. s8 temp;
  362. if (strict_strtol(buf, 10, &reqval))
  363. return -EINVAL;
  364. reqval = SENSORS_LIMIT(reqval, -32000, 31750);
  365. i = reqval / 1000;
  366. f = reqval - (i * 1000);
  367. temp = i << 2;
  368. temp |= f / 250;
  369. mutex_lock(&data->update_lock);
  370. data->reg[param->msb[0]] = temp;
  371. write_byte(client, param->msb[0], temp);
  372. mutex_unlock(&data->update_lock);
  373. return count;
  374. }
  375. /*
  376. * The aSC7621 doesn't provide an "auto_point2". Instead, you
  377. * specify the auto_point1 and a range. To keep with the sysfs
  378. * hwmon specs, we synthesize the auto_point_2 from them.
  379. */
  380. static u32 asc7621_range_map[] = {
  381. 2000, 2500, 3330, 4000, 5000, 6670, 8000, 10000,
  382. 13330, 16000, 20000, 26670, 32000, 40000, 53330, 80000,
  383. };
  384. static ssize_t show_ap2_temp(struct device *dev,
  385. struct device_attribute *attr, char *buf)
  386. {
  387. SETUP_SHOW_data_param(dev, attr);
  388. long auto_point1;
  389. u8 regval;
  390. int temp;
  391. mutex_lock(&data->update_lock);
  392. auto_point1 = ((s8) data->reg[param->msb[1]]) * 1000;
  393. regval =
  394. ((data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0]);
  395. temp = auto_point1 + asc7621_range_map[SENSORS_LIMIT(regval, 0, 15)];
  396. mutex_unlock(&data->update_lock);
  397. return sprintf(buf, "%d\n", temp);
  398. }
  399. static ssize_t store_ap2_temp(struct device *dev,
  400. struct device_attribute *attr,
  401. const char *buf, size_t count)
  402. {
  403. SETUP_STORE_data_param(dev, attr);
  404. long reqval, auto_point1;
  405. int i;
  406. u8 currval, newval = 0;
  407. if (strict_strtol(buf, 10, &reqval))
  408. return -EINVAL;
  409. mutex_lock(&data->update_lock);
  410. auto_point1 = data->reg[param->msb[1]] * 1000;
  411. reqval = SENSORS_LIMIT(reqval, auto_point1 + 2000, auto_point1 + 80000);
  412. for (i = ARRAY_SIZE(asc7621_range_map) - 1; i >= 0; i--) {
  413. if (reqval >= auto_point1 + asc7621_range_map[i]) {
  414. newval = i;
  415. break;
  416. }
  417. }
  418. newval = (newval & param->mask[0]) << param->shift[0];
  419. currval = read_byte(client, param->msb[0]);
  420. newval |= (currval & ~(param->mask[0] << param->shift[0]));
  421. data->reg[param->msb[0]] = newval;
  422. write_byte(client, param->msb[0], newval);
  423. mutex_unlock(&data->update_lock);
  424. return count;
  425. }
  426. static ssize_t show_pwm_ac(struct device *dev,
  427. struct device_attribute *attr, char *buf)
  428. {
  429. SETUP_SHOW_data_param(dev, attr);
  430. u8 config, altbit, regval;
  431. u8 map[] = {
  432. 0x01, 0x02, 0x04, 0x1f, 0x00, 0x06, 0x07, 0x10,
  433. 0x08, 0x0f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f
  434. };
  435. mutex_lock(&data->update_lock);
  436. config = (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
  437. altbit = (data->reg[param->msb[1]] >> param->shift[1]) & param->mask[1];
  438. regval = config | (altbit << 3);
  439. mutex_unlock(&data->update_lock);
  440. return sprintf(buf, "%u\n", map[SENSORS_LIMIT(regval, 0, 15)]);
  441. }
  442. static ssize_t store_pwm_ac(struct device *dev,
  443. struct device_attribute *attr,
  444. const char *buf, size_t count)
  445. {
  446. SETUP_STORE_data_param(dev, attr);
  447. unsigned long reqval;
  448. u8 currval, config, altbit, newval;
  449. u16 map[] = {
  450. 0x04, 0x00, 0x01, 0xff, 0x02, 0xff, 0x05, 0x06,
  451. 0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
  452. 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  453. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03,
  454. };
  455. if (strict_strtoul(buf, 10, &reqval))
  456. return -EINVAL;
  457. if (reqval > 31)
  458. return -EINVAL;
  459. reqval = map[reqval];
  460. if (reqval == 0xff)
  461. return -EINVAL;
  462. config = reqval & 0x07;
  463. altbit = (reqval >> 3) & 0x01;
  464. config = (config & param->mask[0]) << param->shift[0];
  465. altbit = (altbit & param->mask[1]) << param->shift[1];
  466. mutex_lock(&data->update_lock);
  467. currval = read_byte(client, param->msb[0]);
  468. newval = config | (currval & ~(param->mask[0] << param->shift[0]));
  469. newval = altbit | (newval & ~(param->mask[1] << param->shift[1]));
  470. data->reg[param->msb[0]] = newval;
  471. write_byte(client, param->msb[0], newval);
  472. mutex_unlock(&data->update_lock);
  473. return count;
  474. }
  475. static ssize_t show_pwm_enable(struct device *dev,
  476. struct device_attribute *attr, char *buf)
  477. {
  478. SETUP_SHOW_data_param(dev, attr);
  479. u8 config, altbit, minoff, val, newval;
  480. mutex_lock(&data->update_lock);
  481. config = (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
  482. altbit = (data->reg[param->msb[1]] >> param->shift[1]) & param->mask[1];
  483. minoff = (data->reg[param->msb[2]] >> param->shift[2]) & param->mask[2];
  484. mutex_unlock(&data->update_lock);
  485. val = config | (altbit << 3);
  486. newval = 0;
  487. if (val == 3 || val >= 10)
  488. newval = 255;
  489. else if (val == 4)
  490. newval = 0;
  491. else if (val == 7)
  492. newval = 1;
  493. else if (minoff == 1)
  494. newval = 2;
  495. else
  496. newval = 3;
  497. return sprintf(buf, "%u\n", newval);
  498. }
  499. static ssize_t store_pwm_enable(struct device *dev,
  500. struct device_attribute *attr,
  501. const char *buf, size_t count)
  502. {
  503. SETUP_STORE_data_param(dev, attr);
  504. long reqval;
  505. u8 currval, config, altbit, newval, minoff = 255;
  506. if (strict_strtol(buf, 10, &reqval))
  507. return -EINVAL;
  508. switch (reqval) {
  509. case 0:
  510. newval = 0x04;
  511. break;
  512. case 1:
  513. newval = 0x07;
  514. break;
  515. case 2:
  516. newval = 0x00;
  517. minoff = 1;
  518. break;
  519. case 3:
  520. newval = 0x00;
  521. minoff = 0;
  522. break;
  523. case 255:
  524. newval = 0x03;
  525. break;
  526. default:
  527. return -EINVAL;
  528. }
  529. config = newval & 0x07;
  530. altbit = (newval >> 3) & 0x01;
  531. mutex_lock(&data->update_lock);
  532. config = (config & param->mask[0]) << param->shift[0];
  533. altbit = (altbit & param->mask[1]) << param->shift[1];
  534. currval = read_byte(client, param->msb[0]);
  535. newval = config | (currval & ~(param->mask[0] << param->shift[0]));
  536. newval = altbit | (newval & ~(param->mask[1] << param->shift[1]));
  537. data->reg[param->msb[0]] = newval;
  538. write_byte(client, param->msb[0], newval);
  539. if (minoff < 255) {
  540. minoff = (minoff & param->mask[2]) << param->shift[2];
  541. currval = read_byte(client, param->msb[2]);
  542. newval =
  543. minoff | (currval & ~(param->mask[2] << param->shift[2]));
  544. data->reg[param->msb[2]] = newval;
  545. write_byte(client, param->msb[2], newval);
  546. }
  547. mutex_unlock(&data->update_lock);
  548. return count;
  549. }
  550. static u32 asc7621_pwm_freq_map[] = {
  551. 10, 15, 23, 30, 38, 47, 62, 94,
  552. 23000, 24000, 25000, 26000, 27000, 28000, 29000, 30000
  553. };
  554. static ssize_t show_pwm_freq(struct device *dev,
  555. struct device_attribute *attr, char *buf)
  556. {
  557. SETUP_SHOW_data_param(dev, attr);
  558. u8 regval =
  559. (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
  560. regval = SENSORS_LIMIT(regval, 0, 15);
  561. return sprintf(buf, "%u\n", asc7621_pwm_freq_map[regval]);
  562. }
  563. static ssize_t store_pwm_freq(struct device *dev,
  564. struct device_attribute *attr,
  565. const char *buf, size_t count)
  566. {
  567. SETUP_STORE_data_param(dev, attr);
  568. unsigned long reqval;
  569. u8 currval, newval = 255;
  570. int i;
  571. if (strict_strtoul(buf, 10, &reqval))
  572. return -EINVAL;
  573. for (i = 0; i < ARRAY_SIZE(asc7621_pwm_freq_map); i++) {
  574. if (reqval == asc7621_pwm_freq_map[i]) {
  575. newval = i;
  576. break;
  577. }
  578. }
  579. if (newval == 255)
  580. return -EINVAL;
  581. newval = (newval & param->mask[0]) << param->shift[0];
  582. mutex_lock(&data->update_lock);
  583. currval = read_byte(client, param->msb[0]);
  584. newval |= (currval & ~(param->mask[0] << param->shift[0]));
  585. data->reg[param->msb[0]] = newval;
  586. write_byte(client, param->msb[0], newval);
  587. mutex_unlock(&data->update_lock);
  588. return count;
  589. }
  590. static u32 asc7621_pwm_auto_spinup_map[] = {
  591. 0, 100, 250, 400, 700, 1000, 2000, 4000
  592. };
  593. static ssize_t show_pwm_ast(struct device *dev,
  594. struct device_attribute *attr, char *buf)
  595. {
  596. SETUP_SHOW_data_param(dev, attr);
  597. u8 regval =
  598. (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
  599. regval = SENSORS_LIMIT(regval, 0, 7);
  600. return sprintf(buf, "%u\n", asc7621_pwm_auto_spinup_map[regval]);
  601. }
  602. static ssize_t store_pwm_ast(struct device *dev,
  603. struct device_attribute *attr,
  604. const char *buf, size_t count)
  605. {
  606. SETUP_STORE_data_param(dev, attr);
  607. long reqval;
  608. u8 currval, newval = 255;
  609. u32 i;
  610. if (strict_strtol(buf, 10, &reqval))
  611. return -EINVAL;
  612. for (i = 0; i < ARRAY_SIZE(asc7621_pwm_auto_spinup_map); i++) {
  613. if (reqval == asc7621_pwm_auto_spinup_map[i]) {
  614. newval = i;
  615. break;
  616. }
  617. }
  618. if (newval == 255)
  619. return -EINVAL;
  620. newval = (newval & param->mask[0]) << param->shift[0];
  621. mutex_lock(&data->update_lock);
  622. currval = read_byte(client, param->msb[0]);
  623. newval |= (currval & ~(param->mask[0] << param->shift[0]));
  624. data->reg[param->msb[0]] = newval;
  625. write_byte(client, param->msb[0], newval);
  626. mutex_unlock(&data->update_lock);
  627. return count;
  628. }
  629. static u32 asc7621_temp_smoothing_time_map[] = {
  630. 35000, 17600, 11800, 7000, 4400, 3000, 1600, 800
  631. };
  632. static ssize_t show_temp_st(struct device *dev,
  633. struct device_attribute *attr, char *buf)
  634. {
  635. SETUP_SHOW_data_param(dev, attr);
  636. u8 regval =
  637. (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
  638. regval = SENSORS_LIMIT(regval, 0, 7);
  639. return sprintf(buf, "%u\n", asc7621_temp_smoothing_time_map[regval]);
  640. }
  641. static ssize_t store_temp_st(struct device *dev,
  642. struct device_attribute *attr,
  643. const char *buf, size_t count)
  644. {
  645. SETUP_STORE_data_param(dev, attr);
  646. long reqval;
  647. u8 currval, newval = 255;
  648. u32 i;
  649. if (strict_strtol(buf, 10, &reqval))
  650. return -EINVAL;
  651. for (i = 0; i < ARRAY_SIZE(asc7621_temp_smoothing_time_map); i++) {
  652. if (reqval == asc7621_temp_smoothing_time_map[i]) {
  653. newval = i;
  654. break;
  655. }
  656. }
  657. if (newval == 255)
  658. return -EINVAL;
  659. newval = (newval & param->mask[0]) << param->shift[0];
  660. mutex_lock(&data->update_lock);
  661. currval = read_byte(client, param->msb[0]);
  662. newval |= (currval & ~(param->mask[0] << param->shift[0]));
  663. data->reg[param->msb[0]] = newval;
  664. write_byte(client, param->msb[0], newval);
  665. mutex_unlock(&data->update_lock);
  666. return count;
  667. }
  668. /*
  669. * End of data handlers
  670. *
  671. * These defines do nothing more than make the table easier
  672. * to read when wrapped at column 80.
  673. */
  674. /*
  675. * Creates a variable length array inititalizer.
  676. * VAA(1,3,5,7) would produce {1,3,5,7}
  677. */
  678. #define VAA(args...) {args}
  679. #define PREAD(name, n, pri, rm, rl, m, s, r) \
  680. {.sda = SENSOR_ATTR(name, S_IRUGO, show_##r, NULL, n), \
  681. .priority = pri, .msb[0] = rm, .lsb[0] = rl, .mask[0] = m, \
  682. .shift[0] = s,}
  683. #define PWRITE(name, n, pri, rm, rl, m, s, r) \
  684. {.sda = SENSOR_ATTR(name, S_IRUGO | S_IWUSR, show_##r, store_##r, n), \
  685. .priority = pri, .msb[0] = rm, .lsb[0] = rl, .mask[0] = m, \
  686. .shift[0] = s,}
  687. /*
  688. * PWRITEM assumes that the initializers for the .msb, .lsb, .mask and .shift
  689. * were created using the VAA macro.
  690. */
  691. #define PWRITEM(name, n, pri, rm, rl, m, s, r) \
  692. {.sda = SENSOR_ATTR(name, S_IRUGO | S_IWUSR, show_##r, store_##r, n), \
  693. .priority = pri, .msb = rm, .lsb = rl, .mask = m, .shift = s,}
  694. static struct asc7621_param asc7621_params[] = {
  695. PREAD(in0_input, 0, PRI_HIGH, 0x20, 0x13, 0, 0, in10),
  696. PREAD(in1_input, 1, PRI_HIGH, 0x21, 0x18, 0, 0, in10),
  697. PREAD(in2_input, 2, PRI_HIGH, 0x22, 0x11, 0, 0, in10),
  698. PREAD(in3_input, 3, PRI_HIGH, 0x23, 0x12, 0, 0, in10),
  699. PREAD(in4_input, 4, PRI_HIGH, 0x24, 0x14, 0, 0, in10),
  700. PWRITE(in0_min, 0, PRI_LOW, 0x44, 0, 0, 0, in8),
  701. PWRITE(in1_min, 1, PRI_LOW, 0x46, 0, 0, 0, in8),
  702. PWRITE(in2_min, 2, PRI_LOW, 0x48, 0, 0, 0, in8),
  703. PWRITE(in3_min, 3, PRI_LOW, 0x4a, 0, 0, 0, in8),
  704. PWRITE(in4_min, 4, PRI_LOW, 0x4c, 0, 0, 0, in8),
  705. PWRITE(in0_max, 0, PRI_LOW, 0x45, 0, 0, 0, in8),
  706. PWRITE(in1_max, 1, PRI_LOW, 0x47, 0, 0, 0, in8),
  707. PWRITE(in2_max, 2, PRI_LOW, 0x49, 0, 0, 0, in8),
  708. PWRITE(in3_max, 3, PRI_LOW, 0x4b, 0, 0, 0, in8),
  709. PWRITE(in4_max, 4, PRI_LOW, 0x4d, 0, 0, 0, in8),
  710. PREAD(in0_alarm, 0, PRI_HIGH, 0x41, 0, 0x01, 0, bitmask),
  711. PREAD(in1_alarm, 1, PRI_HIGH, 0x41, 0, 0x01, 1, bitmask),
  712. PREAD(in2_alarm, 2, PRI_HIGH, 0x41, 0, 0x01, 2, bitmask),
  713. PREAD(in3_alarm, 3, PRI_HIGH, 0x41, 0, 0x01, 3, bitmask),
  714. PREAD(in4_alarm, 4, PRI_HIGH, 0x42, 0, 0x01, 0, bitmask),
  715. PREAD(fan1_input, 0, PRI_HIGH, 0x29, 0x28, 0, 0, fan16),
  716. PREAD(fan2_input, 1, PRI_HIGH, 0x2b, 0x2a, 0, 0, fan16),
  717. PREAD(fan3_input, 2, PRI_HIGH, 0x2d, 0x2c, 0, 0, fan16),
  718. PREAD(fan4_input, 3, PRI_HIGH, 0x2f, 0x2e, 0, 0, fan16),
  719. PWRITE(fan1_min, 0, PRI_LOW, 0x55, 0x54, 0, 0, fan16),
  720. PWRITE(fan2_min, 1, PRI_LOW, 0x57, 0x56, 0, 0, fan16),
  721. PWRITE(fan3_min, 2, PRI_LOW, 0x59, 0x58, 0, 0, fan16),
  722. PWRITE(fan4_min, 3, PRI_LOW, 0x5b, 0x5a, 0, 0, fan16),
  723. PREAD(fan1_alarm, 0, PRI_HIGH, 0x42, 0, 0x01, 2, bitmask),
  724. PREAD(fan2_alarm, 1, PRI_HIGH, 0x42, 0, 0x01, 3, bitmask),
  725. PREAD(fan3_alarm, 2, PRI_HIGH, 0x42, 0, 0x01, 4, bitmask),
  726. PREAD(fan4_alarm, 3, PRI_HIGH, 0x42, 0, 0x01, 5, bitmask),
  727. PREAD(temp1_input, 0, PRI_HIGH, 0x25, 0x10, 0, 0, temp10),
  728. PREAD(temp2_input, 1, PRI_HIGH, 0x26, 0x15, 0, 0, temp10),
  729. PREAD(temp3_input, 2, PRI_HIGH, 0x27, 0x16, 0, 0, temp10),
  730. PREAD(temp4_input, 3, PRI_HIGH, 0x33, 0x17, 0, 0, temp10),
  731. PREAD(temp5_input, 4, PRI_HIGH, 0xf7, 0xf6, 0, 0, temp10),
  732. PREAD(temp6_input, 5, PRI_HIGH, 0xf9, 0xf8, 0, 0, temp10),
  733. PREAD(temp7_input, 6, PRI_HIGH, 0xfb, 0xfa, 0, 0, temp10),
  734. PREAD(temp8_input, 7, PRI_HIGH, 0xfd, 0xfc, 0, 0, temp10),
  735. PWRITE(temp1_min, 0, PRI_LOW, 0x4e, 0, 0, 0, temp8),
  736. PWRITE(temp2_min, 1, PRI_LOW, 0x50, 0, 0, 0, temp8),
  737. PWRITE(temp3_min, 2, PRI_LOW, 0x52, 0, 0, 0, temp8),
  738. PWRITE(temp4_min, 3, PRI_LOW, 0x34, 0, 0, 0, temp8),
  739. PWRITE(temp1_max, 0, PRI_LOW, 0x4f, 0, 0, 0, temp8),
  740. PWRITE(temp2_max, 1, PRI_LOW, 0x51, 0, 0, 0, temp8),
  741. PWRITE(temp3_max, 2, PRI_LOW, 0x53, 0, 0, 0, temp8),
  742. PWRITE(temp4_max, 3, PRI_LOW, 0x35, 0, 0, 0, temp8),
  743. PREAD(temp1_alarm, 0, PRI_HIGH, 0x41, 0, 0x01, 4, bitmask),
  744. PREAD(temp2_alarm, 1, PRI_HIGH, 0x41, 0, 0x01, 5, bitmask),
  745. PREAD(temp3_alarm, 2, PRI_HIGH, 0x41, 0, 0x01, 6, bitmask),
  746. PREAD(temp4_alarm, 3, PRI_HIGH, 0x43, 0, 0x01, 0, bitmask),
  747. PWRITE(temp1_source, 0, PRI_LOW, 0x02, 0, 0x07, 4, bitmask),
  748. PWRITE(temp2_source, 1, PRI_LOW, 0x02, 0, 0x07, 0, bitmask),
  749. PWRITE(temp3_source, 2, PRI_LOW, 0x03, 0, 0x07, 4, bitmask),
  750. PWRITE(temp4_source, 3, PRI_LOW, 0x03, 0, 0x07, 0, bitmask),
  751. PWRITE(temp1_smoothing_enable, 0, PRI_LOW, 0x62, 0, 0x01, 3, bitmask),
  752. PWRITE(temp2_smoothing_enable, 1, PRI_LOW, 0x63, 0, 0x01, 7, bitmask),
  753. PWRITE(temp3_smoothing_enable, 2, PRI_LOW, 0x63, 0, 0x01, 3, bitmask),
  754. PWRITE(temp4_smoothing_enable, 3, PRI_LOW, 0x3c, 0, 0x01, 3, bitmask),
  755. PWRITE(temp1_smoothing_time, 0, PRI_LOW, 0x62, 0, 0x07, 0, temp_st),
  756. PWRITE(temp2_smoothing_time, 1, PRI_LOW, 0x63, 0, 0x07, 4, temp_st),
  757. PWRITE(temp3_smoothing_time, 2, PRI_LOW, 0x63, 0, 0x07, 0, temp_st),
  758. PWRITE(temp4_smoothing_time, 3, PRI_LOW, 0x3c, 0, 0x07, 0, temp_st),
  759. PWRITE(temp1_auto_point1_temp_hyst, 0, PRI_LOW, 0x6d, 0, 0x0f, 4,
  760. bitmask),
  761. PWRITE(temp2_auto_point1_temp_hyst, 1, PRI_LOW, 0x6d, 0, 0x0f, 0,
  762. bitmask),
  763. PWRITE(temp3_auto_point1_temp_hyst, 2, PRI_LOW, 0x6e, 0, 0x0f, 4,
  764. bitmask),
  765. PWRITE(temp4_auto_point1_temp_hyst, 3, PRI_LOW, 0x6e, 0, 0x0f, 0,
  766. bitmask),
  767. PREAD(temp1_auto_point2_temp_hyst, 0, PRI_LOW, 0x6d, 0, 0x0f, 4,
  768. bitmask),
  769. PREAD(temp2_auto_point2_temp_hyst, 1, PRI_LOW, 0x6d, 0, 0x0f, 0,
  770. bitmask),
  771. PREAD(temp3_auto_point2_temp_hyst, 2, PRI_LOW, 0x6e, 0, 0x0f, 4,
  772. bitmask),
  773. PREAD(temp4_auto_point2_temp_hyst, 3, PRI_LOW, 0x6e, 0, 0x0f, 0,
  774. bitmask),
  775. PWRITE(temp1_auto_point1_temp, 0, PRI_LOW, 0x67, 0, 0, 0, temp8),
  776. PWRITE(temp2_auto_point1_temp, 1, PRI_LOW, 0x68, 0, 0, 0, temp8),
  777. PWRITE(temp3_auto_point1_temp, 2, PRI_LOW, 0x69, 0, 0, 0, temp8),
  778. PWRITE(temp4_auto_point1_temp, 3, PRI_LOW, 0x3b, 0, 0, 0, temp8),
  779. PWRITEM(temp1_auto_point2_temp, 0, PRI_LOW, VAA(0x5f, 0x67), VAA(0),
  780. VAA(0x0f), VAA(4), ap2_temp),
  781. PWRITEM(temp2_auto_point2_temp, 1, PRI_LOW, VAA(0x60, 0x68), VAA(0),
  782. VAA(0x0f), VAA(4), ap2_temp),
  783. PWRITEM(temp3_auto_point2_temp, 2, PRI_LOW, VAA(0x61, 0x69), VAA(0),
  784. VAA(0x0f), VAA(4), ap2_temp),
  785. PWRITEM(temp4_auto_point2_temp, 3, PRI_LOW, VAA(0x3c, 0x3b), VAA(0),
  786. VAA(0x0f), VAA(4), ap2_temp),
  787. PWRITE(temp1_crit, 0, PRI_LOW, 0x6a, 0, 0, 0, temp8),
  788. PWRITE(temp2_crit, 1, PRI_LOW, 0x6b, 0, 0, 0, temp8),
  789. PWRITE(temp3_crit, 2, PRI_LOW, 0x6c, 0, 0, 0, temp8),
  790. PWRITE(temp4_crit, 3, PRI_LOW, 0x3d, 0, 0, 0, temp8),
  791. PWRITE(temp5_enable, 4, PRI_LOW, 0x0e, 0, 0x01, 0, bitmask),
  792. PWRITE(temp6_enable, 5, PRI_LOW, 0x0e, 0, 0x01, 1, bitmask),
  793. PWRITE(temp7_enable, 6, PRI_LOW, 0x0e, 0, 0x01, 2, bitmask),
  794. PWRITE(temp8_enable, 7, PRI_LOW, 0x0e, 0, 0x01, 3, bitmask),
  795. PWRITE(remote1_offset, 0, PRI_LOW, 0x1c, 0, 0, 0, temp62),
  796. PWRITE(remote2_offset, 1, PRI_LOW, 0x1d, 0, 0, 0, temp62),
  797. PWRITE(pwm1, 0, PRI_HIGH, 0x30, 0, 0, 0, u8),
  798. PWRITE(pwm2, 1, PRI_HIGH, 0x31, 0, 0, 0, u8),
  799. PWRITE(pwm3, 2, PRI_HIGH, 0x32, 0, 0, 0, u8),
  800. PWRITE(pwm1_invert, 0, PRI_LOW, 0x5c, 0, 0x01, 4, bitmask),
  801. PWRITE(pwm2_invert, 1, PRI_LOW, 0x5d, 0, 0x01, 4, bitmask),
  802. PWRITE(pwm3_invert, 2, PRI_LOW, 0x5e, 0, 0x01, 4, bitmask),
  803. PWRITEM(pwm1_enable, 0, PRI_LOW, VAA(0x5c, 0x5c, 0x62), VAA(0, 0, 0),
  804. VAA(0x07, 0x01, 0x01), VAA(5, 3, 5), pwm_enable),
  805. PWRITEM(pwm2_enable, 1, PRI_LOW, VAA(0x5d, 0x5d, 0x62), VAA(0, 0, 0),
  806. VAA(0x07, 0x01, 0x01), VAA(5, 3, 6), pwm_enable),
  807. PWRITEM(pwm3_enable, 2, PRI_LOW, VAA(0x5e, 0x5e, 0x62), VAA(0, 0, 0),
  808. VAA(0x07, 0x01, 0x01), VAA(5, 3, 7), pwm_enable),
  809. PWRITEM(pwm1_auto_channels, 0, PRI_LOW, VAA(0x5c, 0x5c), VAA(0, 0),
  810. VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
  811. PWRITEM(pwm2_auto_channels, 1, PRI_LOW, VAA(0x5d, 0x5d), VAA(0, 0),
  812. VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
  813. PWRITEM(pwm3_auto_channels, 2, PRI_LOW, VAA(0x5e, 0x5e), VAA(0, 0),
  814. VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
  815. PWRITE(pwm1_auto_point1_pwm, 0, PRI_LOW, 0x64, 0, 0, 0, u8),
  816. PWRITE(pwm2_auto_point1_pwm, 1, PRI_LOW, 0x65, 0, 0, 0, u8),
  817. PWRITE(pwm3_auto_point1_pwm, 2, PRI_LOW, 0x66, 0, 0, 0, u8),
  818. PWRITE(pwm1_auto_point2_pwm, 0, PRI_LOW, 0x38, 0, 0, 0, u8),
  819. PWRITE(pwm2_auto_point2_pwm, 1, PRI_LOW, 0x39, 0, 0, 0, u8),
  820. PWRITE(pwm3_auto_point2_pwm, 2, PRI_LOW, 0x3a, 0, 0, 0, u8),
  821. PWRITE(pwm1_freq, 0, PRI_LOW, 0x5f, 0, 0x0f, 0, pwm_freq),
  822. PWRITE(pwm2_freq, 1, PRI_LOW, 0x60, 0, 0x0f, 0, pwm_freq),
  823. PWRITE(pwm3_freq, 2, PRI_LOW, 0x61, 0, 0x0f, 0, pwm_freq),
  824. PREAD(pwm1_auto_zone_assigned, 0, PRI_LOW, 0, 0, 0x03, 2, bitmask),
  825. PREAD(pwm2_auto_zone_assigned, 1, PRI_LOW, 0, 0, 0x03, 4, bitmask),
  826. PREAD(pwm3_auto_zone_assigned, 2, PRI_LOW, 0, 0, 0x03, 6, bitmask),
  827. PWRITE(pwm1_auto_spinup_time, 0, PRI_LOW, 0x5c, 0, 0x07, 0, pwm_ast),
  828. PWRITE(pwm2_auto_spinup_time, 1, PRI_LOW, 0x5d, 0, 0x07, 0, pwm_ast),
  829. PWRITE(pwm3_auto_spinup_time, 2, PRI_LOW, 0x5e, 0, 0x07, 0, pwm_ast),
  830. PWRITE(peci_enable, 0, PRI_LOW, 0x40, 0, 0x01, 4, bitmask),
  831. PWRITE(peci_avg, 0, PRI_LOW, 0x36, 0, 0x07, 0, bitmask),
  832. PWRITE(peci_domain, 0, PRI_LOW, 0x36, 0, 0x01, 3, bitmask),
  833. PWRITE(peci_legacy, 0, PRI_LOW, 0x36, 0, 0x01, 4, bitmask),
  834. PWRITE(peci_diode, 0, PRI_LOW, 0x0e, 0, 0x07, 4, bitmask),
  835. PWRITE(peci_4domain, 0, PRI_LOW, 0x0e, 0, 0x01, 4, bitmask),
  836. };
  837. static struct asc7621_data *asc7621_update_device(struct device *dev)
  838. {
  839. struct i2c_client *client = to_i2c_client(dev);
  840. struct asc7621_data *data = i2c_get_clientdata(client);
  841. int i;
  842. /*
  843. * The asc7621 chips guarantee consistent reads of multi-byte values
  844. * regardless of the order of the reads. No special logic is needed
  845. * so we can just read the registers in whatever order they appear
  846. * in the asc7621_params array.
  847. */
  848. mutex_lock(&data->update_lock);
  849. /* Read all the high priority registers */
  850. if (!data->valid ||
  851. time_after(jiffies, data->last_high_reading + INTERVAL_HIGH)) {
  852. for (i = 0; i < ARRAY_SIZE(asc7621_register_priorities); i++) {
  853. if (asc7621_register_priorities[i] == PRI_HIGH) {
  854. data->reg[i] =
  855. i2c_smbus_read_byte_data(client, i) & 0xff;
  856. }
  857. }
  858. data->last_high_reading = jiffies;
  859. }; /* last_reading */
  860. /* Read all the low priority registers. */
  861. if (!data->valid ||
  862. time_after(jiffies, data->last_low_reading + INTERVAL_LOW)) {
  863. for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
  864. if (asc7621_register_priorities[i] == PRI_LOW) {
  865. data->reg[i] =
  866. i2c_smbus_read_byte_data(client, i) & 0xff;
  867. }
  868. }
  869. data->last_low_reading = jiffies;
  870. }; /* last_reading */
  871. data->valid = 1;
  872. mutex_unlock(&data->update_lock);
  873. return data;
  874. }
  875. /*
  876. * Standard detection and initialization below
  877. *
  878. * Helper function that checks if an address is valid
  879. * for a particular chip.
  880. */
  881. static inline int valid_address_for_chip(int chip_type, int address)
  882. {
  883. int i;
  884. for (i = 0; asc7621_chips[chip_type].addresses[i] != I2C_CLIENT_END;
  885. i++) {
  886. if (asc7621_chips[chip_type].addresses[i] == address)
  887. return 1;
  888. }
  889. return 0;
  890. }
  891. static void asc7621_init_client(struct i2c_client *client)
  892. {
  893. int value;
  894. /* Warn if part was not "READY" */
  895. value = read_byte(client, 0x40);
  896. if (value & 0x02) {
  897. dev_err(&client->dev,
  898. "Client (%d,0x%02x) config is locked.\n",
  899. i2c_adapter_id(client->adapter), client->addr);
  900. };
  901. if (!(value & 0x04)) {
  902. dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
  903. i2c_adapter_id(client->adapter), client->addr);
  904. };
  905. /*
  906. * Start monitoring
  907. *
  908. * Try to clear LOCK, Set START, save everything else
  909. */
  910. value = (value & ~0x02) | 0x01;
  911. write_byte(client, 0x40, value & 0xff);
  912. }
  913. static int
  914. asc7621_probe(struct i2c_client *client, const struct i2c_device_id *id)
  915. {
  916. struct asc7621_data *data;
  917. int i, err;
  918. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  919. return -EIO;
  920. data = kzalloc(sizeof(struct asc7621_data), GFP_KERNEL);
  921. if (data == NULL)
  922. return -ENOMEM;
  923. i2c_set_clientdata(client, data);
  924. data->valid = 0;
  925. mutex_init(&data->update_lock);
  926. /* Initialize the asc7621 chip */
  927. asc7621_init_client(client);
  928. /* Create the sysfs entries */
  929. for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
  930. err =
  931. device_create_file(&client->dev,
  932. &(asc7621_params[i].sda.dev_attr));
  933. if (err)
  934. goto exit_remove;
  935. }
  936. data->class_dev = hwmon_device_register(&client->dev);
  937. if (IS_ERR(data->class_dev)) {
  938. err = PTR_ERR(data->class_dev);
  939. goto exit_remove;
  940. }
  941. return 0;
  942. exit_remove:
  943. for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
  944. device_remove_file(&client->dev,
  945. &(asc7621_params[i].sda.dev_attr));
  946. }
  947. kfree(data);
  948. return err;
  949. }
  950. static int asc7621_detect(struct i2c_client *client,
  951. struct i2c_board_info *info)
  952. {
  953. struct i2c_adapter *adapter = client->adapter;
  954. int company, verstep, chip_index;
  955. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  956. return -ENODEV;
  957. for (chip_index = FIRST_CHIP; chip_index <= LAST_CHIP; chip_index++) {
  958. if (!valid_address_for_chip(chip_index, client->addr))
  959. continue;
  960. company = read_byte(client,
  961. asc7621_chips[chip_index].company_reg);
  962. verstep = read_byte(client,
  963. asc7621_chips[chip_index].verstep_reg);
  964. if (company == asc7621_chips[chip_index].company_id &&
  965. verstep == asc7621_chips[chip_index].verstep_id) {
  966. strlcpy(info->type, asc7621_chips[chip_index].name,
  967. I2C_NAME_SIZE);
  968. dev_info(&adapter->dev, "Matched %s at 0x%02x\n",
  969. asc7621_chips[chip_index].name, client->addr);
  970. return 0;
  971. }
  972. }
  973. return -ENODEV;
  974. }
  975. static int asc7621_remove(struct i2c_client *client)
  976. {
  977. struct asc7621_data *data = i2c_get_clientdata(client);
  978. int i;
  979. hwmon_device_unregister(data->class_dev);
  980. for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
  981. device_remove_file(&client->dev,
  982. &(asc7621_params[i].sda.dev_attr));
  983. }
  984. kfree(data);
  985. return 0;
  986. }
  987. static const struct i2c_device_id asc7621_id[] = {
  988. {"asc7621", asc7621},
  989. {"asc7621a", asc7621a},
  990. {},
  991. };
  992. MODULE_DEVICE_TABLE(i2c, asc7621_id);
  993. static struct i2c_driver asc7621_driver = {
  994. .class = I2C_CLASS_HWMON,
  995. .driver = {
  996. .name = "asc7621",
  997. },
  998. .probe = asc7621_probe,
  999. .remove = asc7621_remove,
  1000. .id_table = asc7621_id,
  1001. .detect = asc7621_detect,
  1002. .address_list = normal_i2c,
  1003. };
  1004. static int __init sm_asc7621_init(void)
  1005. {
  1006. int i, j;
  1007. /*
  1008. * Collect all the registers needed into a single array.
  1009. * This way, if a register isn't actually used for anything,
  1010. * we don't retrieve it.
  1011. */
  1012. for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
  1013. for (j = 0; j < ARRAY_SIZE(asc7621_params[i].msb); j++)
  1014. asc7621_register_priorities[asc7621_params[i].msb[j]] =
  1015. asc7621_params[i].priority;
  1016. for (j = 0; j < ARRAY_SIZE(asc7621_params[i].lsb); j++)
  1017. asc7621_register_priorities[asc7621_params[i].lsb[j]] =
  1018. asc7621_params[i].priority;
  1019. }
  1020. return i2c_add_driver(&asc7621_driver);
  1021. }
  1022. static void __exit sm_asc7621_exit(void)
  1023. {
  1024. i2c_del_driver(&asc7621_driver);
  1025. }
  1026. MODULE_LICENSE("GPL");
  1027. MODULE_AUTHOR("George Joseph");
  1028. MODULE_DESCRIPTION("Andigilog aSC7621 and aSC7621a driver");
  1029. module_init(sm_asc7621_init);
  1030. module_exit(sm_asc7621_exit);